Uploading Images & Videos
Astro Cloudinary provides a seamless way to integrate Cloudinary’s powerful upload capabilities into your Astro project. This guide will show you how to use the CldUploadWidget
component to add upload functionality to your application.
CldUploadWidget Component
The CldUploadWidget
component is a wrapper around Cloudinary’s Upload Widget, specifically optimized for Astro projects. It allows you to easily add upload functionality with minimal configuration.
Basic Usage
Here’s a simple example of how to use the CldUploadWidget
:
Replace <Your Upload Preset>
with your actual Cloudinary upload preset.
Live Example
Here’s a live example of the CldUploadWidget in action:
Signed Uploads
For enhanced security, you can use signed uploads. This requires setting up a server-side endpoint to sign the upload parameters.
You’ll need to create an API route to handle the signature. Here’s an example of how your API route might look:
Configuration Options
The CldUploadWidget
component accepts several props for customization:
uploadPreset
: (Required) Your Cloudinary upload presetsignatureEndpoint
: (Optional) Endpoint for signed uploadsonUpload
: Callback function triggered after a successful uploadonError
: Callback function triggered if an error occurs during upload
For a full list of options, refer to the CldUploadWidget Configuration page.
Best Practices and Tips
- Always use upload presets to control what can be uploaded to your Cloudinary account.
- Implement signed uploads for enhanced security, especially in production environments.
- Use the
onUpload
callback to handle successful uploads, such as displaying the uploaded image or updating your application state. - Customize the upload widget to match your application’s design using CSS.
- Consider implementing upload restrictions (file types, sizes) using Cloudinary’s upload presets.