pjholmes

pjholmes

How to enable LiveView File Upload in a component?

I saw that PR had been applied to enable LiveView File Upload, so I thought I would try it out. I’m relatively new to Elixir, Phoenix, LiveView, so what could possibly go wrong?

The guide makes it clear where to add the call to allow_update/3 for a LiveView, but I would like to add it to a form_component that opens in a modal window. I added a mount/1 function to the component below.

   defmodule LiveUploadWeb.PostLive.FormComponent do
      use LiveUploadWeb, :live_component
      alias LiveUpload.Blog

      @impl true

      def mount(socket) do
         IO.puts("in mount")

         newSocket =
            socket
            |> assign(:uploaded_files, [])
            |> allow_upload(:avatar, accept: ~w(.jpg .jpeg .png), max_entries: 1)

         IO.inspect(newSocket, label: "newSocket")

         {:ok, newSocket}
      end
   ...

The mount/1 function is called, and the uploaded_files and uploads assigns are added to the session.

in mount
Socket: #Phoenix.LiveView.Socket<
  assigns: %{
    flash: %{},
    myself: %Phoenix.LiveComponent.CID{cid: 2},
    uploaded_files: [],
    uploads: %{
      __phoenix_refs_to_names__: %{"phx-FkYRxX4PVOACIAIJ" => :avatar},
      avatar: #Phoenix.LiveView.UploadConfig<accept: ".jpg,.jpeg,.png", ...>
    }
  },

However, when I run the server, go to /posts, click ‘New Post’, click “Choose File” and select a file, I get an error message on the sever 'no uploads have been allowed’.

Can someone please explain how to allow uploads in a stateful component?

The repo is here. I explained each of the steps I followed in the README.md file. The README.md also includes the detailed error message.

Most Liked

c4710n

c4710n

To clarify, Live Upload is supported in LiveView and LiveComponent in 2022. It has well support on server-side uploading and client-side direct uploading.

If you encounter any problems, check:

  • allow_upload/3
  • phx-target attribute of form element if you are using LiveComponent

The example provided by @outlog is recommended.

andreaseriksson

andreaseriksson

Its not yet supported

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New

We're in Beta

About us Mission Statement