Getting Started with CldUploadWidget
The CldUploadWidget creates a new instance of the Cloudinary Upload Widget giving you an easy way to add upload capabilities to your Astro project.
Basic Usage
The CldUploadWidget will not render any UI by default. It will instead only render what’s passed through in a <slot />
.
By default, the widget looks for any <button>
nested in the Slot, where upon clicking that button, the widget is opened.
Throughout the upload lifecycle, including selecting an asset, uploading an asset, and more, events are triggered on the component instance, allowing you to listen for those events and update the UI or data store with the results.
Uploading to Cloudinary
There are two options when using the CldUploadWidget: signed and unsigned. These options allow you to control the amount of security and restrictions you place on uploads.
Unsigned
To give unsigned access for upload, provide an upload preset as part of the component configuration.
Use the following to generate an unsigned upload widget:
Signed
Signing upload requests requires passing in an endpoint to the component.
You can do this by creating a serverless function that reads the parameters as the body and returns an object with the signature.
Use the following to generate an signed upload widget:
Here’s an example of how you could process the signature in an API endpoint that signs a request:
To use the above, create a Node-based API route, add the snippet, and use that endpoint as the signatureEndpoint
prop.
Learn more about signing uploads.
Or see a full example of an API endpoint used with the Astro Cloudinary docs: https://github.com/cloudinary-community/astro-cloudinary/
Upload Events
The Cloudinary Upload Widget emits a series of events throughout the upload lifecycle. Some of those events include adding an asset, starting the upload queue (in the event of multiple uploads), and upon successful upload.
You can add callbacks for these events by adding an event listener to widget instance based on an associated ID.
For instance, to listen for the successful upload of an asset, you can add:
To see a complete list of events and the corresponding shape of results, visit the Cloudinary Upload Widget API Reference.
The events are triggered with a pattern of clduploadwidget:<event>
.
All events include a reference to the Upload Widget instance, which exposes instance
methods such as open
and close
.
To see a full list of instance methods, visit the Cloudinary Upload Widget API Reference.