larshei
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:
- I need to allow file uploads:
This will add andef mount(params, session, socket) do {:ok, socket |> allow_upload(:csv, accept: ~w(.csv), max_entries: 1)} end@uploadsassign that holds a description of the constraints and, once selected, files and upload progress. - And can use the
live_file_inputto render a file upload button:
Note: Im using Surface UI with Phoenix Live View 0.16.4def 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
This renders a “Choose File…” button as expected and shows no file selected next to it. - Consume an upload using consume_uploaded_entries. I did not get to this point.
Expectations
With the setup above, I expected
- Clicking the “Choose File” button opens a file selector and
- when rendering
@uploads.csvafter selecting a file it contains information on the selected file in its:entryfield.
What happens
- Clicking the “Choose File” button does not do anything in Chrome. In Safari, a file browser is opened.
- After selecting a file in Safari, the
@uploadsassign 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 ![]()
Solutions
Solution to missing info in
@uploadassigns:
As @Xty mentioned below, aphx-changeis required on the form (which can simply return{:noreply, socket}) to properly update the@uploadsassign.
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
formelement.Have you tried putting the
live_file_inputinside 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-changehandler 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.
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-changewas missing as you mentioned. Adding it to the form now updates the socket data correctly, so it fixed one of my two issuesNow that you mention it, I recognized this is actually mentioned in the docs
However, Chrome still does not work at all, as no file browser is opened on button click.
larshei
For Chrome, drag&drop with the
phx-drop-targetworks, just the button does not.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
labelelement in place of yourdiv.When I change my element to
div, drop works but click stops working. Even thesectionelement 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.kartheek
I use this code and it works in safari and chrome (I use bootstrap for css). I had an error which says some id does not match on upload - but that was because I was creating two livesockets in a single page due two different js files(which I included by mistake).
Also I had to add
phx-change="validate"for validate to trigger.I derived this code from the live_view docs.
OP included solution in his post.