larshei

larshei

Live_file_input does not work

Somehow, I cannot manage to get file uploads to work.

I have read the docs and watched the videos from Chris and also a tutorial at pragmatic studio.

So far, I understand the following:

  1. I need to allow file uploads:
    def mount(params, session, socket) do
      {:ok, socket |> allow_upload(:csv, accept: ~w(.csv), max_entries: 1)}
    end
    
    This will add an @uploads assign that holds a description of the constraints and, once selected, files and upload progress.
  2. And can use the live_file_input to render a file upload button:
    def render(assigns) do
      ~F"""
        <form id="id" for={:data} phx-submit="upload">
          <div class="ui segment" phx-drop-target={@uploads.csv.ref}>
            {live_file_input @uploads.csv}
           </div>
           <p>{inspect @uploads.csv}</p>
           <Button text={gettext "Upload"} icon="upload" extra_attributes="labeled violet"/>
         </form>
      """
    end
    
    Note: Im using Surface UI with Phoenix Live View 0.16.4
    This renders a “Choose File…” button as expected and shows no file selected next to it.
  3. Consume an upload using consume_uploaded_entries. I did not get to this point.

Expectations

With the setup above, I expected

  1. Clicking the “Choose File” button opens a file selector and
  2. when rendering @uploads.csv after selecting a file it contains information on the selected file in its :entry field.

What happens

  1. Clicking the “Choose File” button does not do anything in Chrome. In Safari, a file browser is opened.
  2. After selecting a file in Safari, the @uploads assign seems to not contain anything (<p>{inspect @uploads.csv}</p>):
    #Phoenix.LiveView.UploadConfig<
     accept: ".csv", auto_upload?: false, entries: [], errors: [], max_entries: 1, max_file_size: 8000000, name: :csv, progress_event: nil, ref: "phx-FtoOkG01s4jRVQ-F", ...
    >
    

I am not sure if something is missing and would appreciate ideas/hints on what to check :slight_smile:

Solutions

Solution to missing info in @upload assigns:
As @Xty mentioned below, a phx-change is required on the form (which can simply return {:noreply, socket}) to properly update the @uploads assign.

Marked As Solved

Xty

Xty

It’s hard to tell if the code samples you’ve provided are complete or boiled down examples, but I’m fairly sure that the LiveView upload features are expected to be used from within a form element.

Have you tried putting the live_file_input inside of a form? If not, I think that would be a good place to start.

Another thing that has caused me issues with live uploads is not having a phx-change handler on the form. Even if that handler does nothing but respond {:noreply, socket} it’s still needed for live uploads.

I’d recommend reading through the official documentation if you haven’t, it should cover everything you need and is likely more up-to-date than the videos you’ve mentioned.

https://hexdocs.pm/phoenix_live_view/uploads.html

Also Liked

larshei

larshei

You are right, it was inside a form.
I minimized the example a bit too much and have added the form to the initial post.

phx-change was missing as you mentioned. Adding it to the form now updates the socket data correctly, so it fixed one of my two issues :slight_smile:

Now that you mention it, I recognized this is actually mentioned in the docs :see_no_evil:

Important: You must bind phx-submit and phx-change on the form.

However, Chrome still does not work at all, as no file browser is opened on button click.

03juan

03juan

I just tested this with my Chrome file inputs, because I’ve had no problems clicking it, and realised that I’m using a label element in place of your div.

When I change my element to div, drop works but click stops working. Even the section element in the Upload Entries doc example doesn’t work with Chrome. This is either a browser bug or something in the live_view.js, not really sure.

Where Next?

Popular in Questions Top

earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement