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.

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

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement