svilen
I’m currently implementing the brand new LiveView Uploads, using the auto_upload setting, and I have a situation which I’m not sure how to resolve.
The uploader is configured to handle multiple images, with auto_upload: true:
socket
|> allow_upload(:images,
accept: [".jpg", ".jpeg", ".png"],
auto_upload: true,
progress: &handle_progress/3,
max_file_size: 5 * 1024 * 1024,
max_entries: 20
)
The actual file upoad input is hidden, so we have drag and drop only.
When you drop a bunch a files, and they are all valid, the upload starts and everything works as expected. However, if there is a single file that’s invalid, the whole batch of files stops. I have implemented a cancel button so you can cancel individual files from the UI:
def handle_event("cancel-upload", %{"ref" => ref}, socket) do
{:noreply, cancel_upload(socket, :images, ref)}
end
The problem I’m facing is that after I cancel the invalid file(s), the remaining valid files does not resume uploading. Unless I submit the form or add drag and drop more files, nothing will get uploaded.
I’m looking for a way to “retry” uploading files or trigger auto_upload again, if all entries on the uploader are valid. Does anyone have any ideas how to do it?
Here is my template markup (simplified version) just for completeness:
<%= live_file_input @uploads.images, class: "is-hidden" %>
<div class="dropzone" phx-hook="DragOver" phx-drop-target="<%= @uploads.images.ref %>">
Drag and drop your files here
</div>
<table class="uploads-in-progress table">
<tbody>
<%= for entry <- @uploads.images.entries do %>
<tr>
<td class="<%= if !entry.valid?, do: "has-text-danger" %>"><%= entry.client_name %></td>
<td><progress class="progress" value="<%= entry.progress %>" max="100"><%= entry.progress %></progress></td>
<td class="has-text-danger"><span class="is-clickable" phx-click="cancel-upload" phx-value-ref="<%= entry.ref %>">Cancel</span></td>
</tr>
<% end %>
</tbody>
</table>
Trending in Questions
Other Trending Topics
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
svilen
I have a workaround, which is a bit of a hack… Here it is for anyone interested:
I noticed that
live_view.jssimply dispatches aninputevent to the drop target here so I created a hook that triggers the event:The hook subscribes to a
resume_uploadevent, which is triggered when you cancel an upload:and in the template I just had to find a place for the hook:
I wonder if there is a better way to accomplish this
envixo
Thanks for sharing the solution.
lessless
Hi guys,
We are facing the similar issue and I wonder what exactly is going on here and if this is how uploads should be unstuck?
We would like to keep failed uploads in the list and ideally they shouldn’t affect other uploads at all.
enkr1
This workaround is working fine but im just curious why is this not fixed/handled in the phoenix live view?
linusdm
I’m not sure if this is of any help, but I’ve come across this issue too a while back. After a nudge of @chrismccord I found inspiration for a solution in the LiveBeats sources:
https://github.com/fly-apps/live_beats/blob/master/lib/live_beats_web/live/profile_live/upload_form_component.ex#LL43C16-L43C36
To me it’s also unclear why live file uploads behave as they do when an error happens, so I can’t clear away any fog there…
emadalam
Stumbled upon this post while trying to see if there was any better way than what I had already implemented. This is what I do for now and so far it works nicely without the need to call cancel on individual entries.
Furthermore, using the
:invalid_entriesassigns we can also show the feedback on the UI for all the ignored invalid entries.Though everything works for now, it still feels a little odd to be manipulating the internals of the live upload assigns, maybe there is a better way now in 2023?
I did check this out after I stumbled upon this post. However upon digging deeper I saw a lot is going on inside the lv
cancel_uploadimplementation. I’m no expert so I can’t comment on the performance implication of this, but for my use case I allow folder uploads viawebkitdirectoryattribute on live file input and I did see usingcancel_uploadversion on individual entries took a little while longer for a folder with a few thousand files, at least on the dev setup. Maybe an expert can comment more on this.Also, extending the original question, there’s one use case I’m unable to figure out yet and that is how to retry a failed upload for the
:external_client_failureerrors for individual files with other successful uploads in the same list.