LiveView supports interactive file uploads with progress for both direct-to-server uploads and direct-to-cloud external uploads on the client.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phoenixframework/phoenix_live_view/llms.txt
Use this file to discover all available pages before exploring further.
Built-in Features
- Accept specification - Define accepted file types, max number of entries, max file size, etc. When the client selects files, the file metadata is automatically validated against the specification.
- Reactive entries - Uploads are populated in an
@uploadsassign in the socket. Entries automatically respond to progress, errors, cancellation, etc. - Drag and drop - Use the
phx-drop-targetattribute to enable drag-and-drop functionality.
Allow Uploads
You enable an upload, typically on mount, viaallow_upload/3.
Available Options
Theallow_upload/3 function accepts these options:
:accept- List of accepted file extensions or MIME types:max_entries- Maximum number of files (default: 1):max_file_size- Maximum file size in bytes (default: 8MB):auto_upload- Automatically upload when files are selected (default: false):chunk_size- Size of upload chunks in bytes:progress- Custom progress callback:external- Function for external upload configuration
Render Upload Elements
Basic Form
Use thePhoenix.Component.live_file_input/1 component to render a file input:
Upload Entries
Uploads are populated in an@uploads assign. Each allowed upload contains a list of entries with information about progress, client file info, errors, etc.
Drag and Drop Styling
Phoenix LiveView adds thephx-drop-target-active class to the drop target element when a user is dragging a file over it.
TailwindCSS Custom Variant
Create a custom variant for styling during drag:This variant can be used with Tailwind’s arbitrary state selectors to style not just the element itself, but the entire page, sibling elements, parent elements, and more.
Entry Validation
Validation occurs automatically based on conditions specified inallow_upload/3. You must implement at least a minimal phx-change callback:
Error Handling
Entries for files that don’t match the spec will contain errors. Use helper functions to render friendly error messages:Cancel an Entry
Users can cancel upload entries programmatically or via user action:Consume Uploaded Entries
When the user submits a form, the JavaScript client uploads the files first, then invokes thephx-submit callback.
Basic Example
While client metadata cannot be trusted, max file size validations are enforced as each chunk is received when performing direct-to-server uploads.
Static Path Configuration
To access your uploads (e.g., in an<img /> tag), add the uploads directory to static_paths/0. In a vanilla Phoenix project, this is found in lib/my_app_web.ex:
Production Considerations
Storing files directly on the server has limitations in production:- If you’re running multiple instances, the uploaded file will only be on one instance
- Any request routed to another machine will fail
For production, it’s best to store uploads in:
- A database (depending on size and contents)
- A separate storage service (S3, Google Cloud Storage, etc.)
- Use external uploads for direct-to-cloud uploads
Complete Example
Here’s a complete LiveView with upload functionality:API Reference
allow_upload/3
Enables file uploads for a LiveView.
consume_uploaded_entries/3
Processes completed uploads.
%{path: path} and the entry, and should return {:ok, value} or {:postpone, value}.
cancel_upload/3
Cancels an upload entry.
upload_errors/1 and upload_errors/2
Returns error atoms for an upload or entry.
Best Practices
- Validate on the server: Never trust client-side validation alone
- Set reasonable limits: Configure
max_entriesandmax_file_sizeappropriately - Handle errors gracefully: Provide clear error messages to users
- Use external uploads for production: For scalability and reliability
- Preview before upload: Use
live_img_preview/1to show image previews - Clean up temporary files: Ensure temporary upload files are properly cleaned up