Installation
Dropzonecomponent with drag-and-drop UIuseConvexUploadhook for upload logic- File management backend functions
- Convex storage integration
- Convex client setup
What’s Included
Drag & Drop
Intuitive drag-and-drop interface with visual feedback
File Validation
Size limits, type restrictions, and error handling
Upload Progress
Visual feedback during upload with success/error states
Convex Storage
Direct upload to Convex with automatic metadata tracking
Setup
Component
Dropzone
File upload component with drag-and-drop interface.Props
Upload state and handlers from
useConvexUpload hook.Additional CSS classes for the dropzone container.
useConvexUpload Hook
Manages file upload state and logic.Options
Allowed MIME types (e.g.,
['image/png', 'image/jpeg']). Wildcards supported ('image/*'). Empty array allows all types.Maximum file size in bytes (e.g.,
5 * 1024 * 1024 for 5MB).Maximum number of files allowed per upload.
Return Value
Usage Examples
Features
Drag and Drop
Intuitive drag-and-drop interface:- Hover effect when dragging files over dropzone
- Click to browse files
- Visual feedback during drag
File Preview
Shows preview for each file:- Image thumbnails for image files
- File type icons for other files
- File name and size
- Upload status (loading, success, error)
Validation
Client-side validation before upload:- File type checking via MIME types
- File size limits
- Maximum file count
- Clear error messages
Error Handling
Comprehensive error states:- Per-file validation errors
- Upload errors (network, server)
- Visual error indicators
- Retry failed uploads
Backend Functions
generateUploadUrl
Generate a temporary upload URL.string - Temporary URL for file upload
Security Note: This is a public mutation. For production, consider adding rate limiting or authentication.
saveFile
Save file metadata after upload.Storage ID returned from Convex upload
Original filename (max 255 chars)
MIME type of the file
File size in bytes (max 50MB)
Session ID for demo mode (optional)
Id<"files"> - The created file record ID
list
List files for current user or session.getUrl
Get download URL for a file.remove
Delete a file (storage + metadata).Upload Flow
const response = await fetch(uploadUrl, {
method: "POST",
headers: { "Content-Type": file.type },
body: file,
});
const { storageId } = await response.json();
File Types
Supported MIME Types
Common MIME type examples:File Icons
The component shows different icons based on file type:- Images:
ImageIcon+ thumbnail preview - Videos:
FileVideoIcon - Audio:
FileAudioIcon - Text/Documents:
FileTextIcon - Other:
FileIcon
Validation Errors
Common validation error codes:file-invalid-type: File type not allowedfile-too-large: File exceeds size limittoo-many-files: Too many files selected
Customization
Custom Dropzone Style
Custom File Preview
Modify the file card rendering incomponents/dropzone.tsx:
Custom Upload Button
Schema
The files table schema:convex/schema.ts
Security
Security features included:- File size validation (max 50MB backend)
- Filename sanitization
- User/session ownership tracking
- Storage cleanup on file deletion
Demo Mode
Works without authentication:- Each session gets a unique ID
- Files associated with session
- Session ID NOT in localStorage (iframe-safe)
Performance
Direct Upload
Files upload directly to Convex storage (no intermediate server)
Parallel Processing
Multiple files upload in parallel
Retry Logic
Failed uploads can be retried without re-selecting files
Efficient Storage
Convex storage optimized for file delivery
Troubleshooting
Upload fails silently
Upload fails silently
Check browser console for errors. Verify
VITE_CONVEX_URL is set and npx convex dev is running.File size limit exceeded
File size limit exceeded
The backend enforces a 50MB limit. Adjust
maxFileSize in your hook to match or be lower.MIME type errors
MIME type errors
Make sure
allowedMimeTypes matches the files you’re uploading. Use wildcards like 'image/*' for flexibility.Preview images not showing
Preview images not showing
Image previews are generated using
URL.createObjectURL(). Check that the file is a valid image type.Next Steps
Password Auth
Authenticate users before uploading
Realtime Chat
Share files in chat messages
Current User Avatar
Upload profile pictures