Storage.LiveView (PhoenixContribStorage v0.1.0)

View Source

Phoenix LiveView helpers for file uploads.

Summary

Functions

Consumes uploaded entries for a has_many_attached relationship.

Consumes uploaded entries and creates Storage blobs.

Default progress handler for uploads.

Helper for generating file upload configuration.

Functions

consume_uploaded_entries_for_attachment(socket, upload_name, record, attachment_name)

Consumes uploaded entries for a has_many_attached relationship.

Examples

def handle_event("save", %{"post" => post_params}, socket) do
  images =
    consume_uploaded_entries(socket, :images, fn %{path: path}, entry ->
      {:ok, Storage.LiveView.consume_uploaded_entry(path, entry)}
    end)

  Storage.Attachment.attach_many(post, :images, images)
end

consume_uploaded_entry(path, entry)

Consumes uploaded entries and creates Storage blobs.

Examples

def handle_event("save", %{"user" => user_params}, socket) do
  uploaded_files =
    consume_uploaded_entries(socket, :avatar, fn %{path: path}, entry ->
      {:ok, Storage.LiveView.consume_uploaded_entry(path, entry)}
    end)

  # Use uploaded_files with your changeset...
end

handle_progress(arg1, entry, socket)

Default progress handler for uploads.

upload_options(opts \\ [])

Helper for generating file upload configuration.

Examples

def mount(_params, _session, socket) do
  socket =
    socket
    |> allow_upload(:images, Storage.LiveView.upload_options(
      accept: ~w(.jpg .jpeg .png),
      max_entries: 5,
      max_file_size: 5_000_000
    ))

  {:ok, socket}
end