Phoenix.LiveView.allow_upload
allow_upload
, go back to Phoenix.LiveView module for more information.
Allows an upload for the provided name.
Options
:accept
- Required. A list of unique file type specifiers or the atom :any to allow any kind of file. For example,[".jpeg"]
,:any
, etc.:max_entries
- The maximum number of selected files to allow per file input. Defaults to 1.:max_file_size
- The maximum file size in bytes to allow to be uploaded. Defaults 8MB. For example,12_000_000
.:chunk_size
- The chunk size in bytes to send when uploading. Defaults64_000
.:chunk_timeout
- The time in milliseconds to wait before closing the upload channel when a new chunk has not been received. Defaults10_000
.:external
- The 2-arity function for generating metadata for external client uploaders. See the Uploads section for example usage.:progress
- The optional 3-arity function for receiving progress events:auto_upload
- Instructs the client to upload the file automatically on file selection instead of waiting for form submits. Default false.
Raises when a previously allowed upload under the same name is still active.
Examples
allow_upload(socket, :avatar, accept: ~w(.jpg .jpeg), max_entries: 2)
allow_upload(socket, :avatar, accept: :any)
For consuming files automatically as they are uploaded, you can pair auto_upload: true
with
a custom progress function to consume the entries as they are completed. For example:
allow_upload(socket, :avatar, accept: :any, progress: &handle_progress/3, auto_upload: true)
defp handle_progress(:avatar, entry, socket) do
if entry.done? do
uploaded_file =
consume_uploaded_entry(socket, entry, fn %{} = meta ->
...
end)
{:noreply, put_flash(socket, :info, "file #{uploaded_file.name} uploaded")}
else
{:noreply, socket}
end
end