Hello again community,
Following the official docs → Uploads — Phoenix LiveView v0.19.3 as I understand I must first allow my live view uploads in socket:
{:ok, allow_upload(socket, :picture, accept: ~w(.jpg .jpeg .png), max_entries: 1)}
Then, having the @upload
assign I create a form:
<.form let={f} for={@changeset_user_picture}, phx-change="validate" phx-submit="change_user_picture">
<%= live_file_input @uploads.picture %>
<%= submit "Submit" %>
</.form>
As described, I have a minimal callback on validation
def handle_event("validate", _params, socket) do
{:noreply, socket}
end
and a submit handle event
def handle_event("change_user_picture", user_params, socket) do
file_path =
consume_uploaded_entries(socket, :picture, fn %{path: path} ->
dest = Path.join("priv/static/uploads", Path.basename(path))
File.cp!(path, dest)
{:ok, Routes.static_path(socket, "/uploads/#{Path.basename(dest)}")}
end)
(...)
end
Where is my problem? Well, no function clause matching in Phoenix.LiveView.Upload.consume_uploaded_entry/3
. After inspecting @uploads I get:
%{
picture: #Phoenix.LiveView.UploadConfig<
name: :picture,
max_entries: 1,
max_file_size: 8000000,
entries: [
%Phoenix.LiveView.UploadEntry{
progress: 100,
preflighted?: true,
upload_config: :picture,
upload_ref: "phx-F27Jl-8qbdjIZyRE",
ref: "1",
uuid: "99a8a59e-b61c-41b0-8f0c-c20f097430be",
valid?: true,
done?: true,
cancelled?: false,
client_name: "stargate.jpg",
client_size: 159247,
client_type: "image/jpeg",
client_last_modified: nil
}
],
accept: ".jpg,.jpeg,.png",
ref: "phx-F27Jl-8qbdjIZyRE",
errors: [],
auto_upload?: false,
progress_event: nil,
...
>,
__phoenix_refs_to_names__: %{"phx-F27Jl-8qbdjIZyRE" => :picture}
}
which is ok. However, if I try this:
case uploaded_entries(socket, :picture) do
{[_|_] = entries, []} ->
uploaded_files = for entry <- entries do
consume_uploaded_entry(socket, entry, fn %{path: path} ->
dest = Path.join("priv/static/uploads", Path.basename(path))
File.cp!(path, dest)
{:ok, Routes.static_path(socket, "/uploads/#{Path.basename(dest)}")}
end)
end
{:noreply, socket}
_ ->
{:noreply, socket}
end
It occurs that uploaded_entries(socket, :picture)
is empty.
. I’ve watched McCord’s video too, but really can’t find where I am wrong. I’ve read also this thread Live_file_input does not work, but I have a phx-change.
What I am doing wrong? Best regards
EDIT: phx version 1.6