pavancse17

pavancse17

LiveView live_file_input with auto_upload + save

Hi Devs,

I am using Uploads — Phoenix LiveView v0.15.4 (hexdocs.pm) feature in my application. I am using it with auto_upload: true option like below:

        allow_upload(:excel_file,
        accept: ~w(.xlsx),
        max_entries: 1,
        auto_upload: true,
        progress: &handle_progress/3

My requirement is I need to validate the file on fly before submitting the form. I am able to achieve this pretty easily with auto_upload. But I need to save file only after form gets submitted. I am doing it in not so efficient way. My solution for this is as below:

  • Validate the file. If the file is valid copy it to temporary folder.
  • Upon submitting the form save file to s3 bucket and remove it from temporary folder.

Here if we observe Phoenix live view automatically keeps it in temp folder with auto_upload option. but I am unable to use it on form submit because that live file upload process is deleting it on exiting the process. Is there a way to use the same temp file created by phoenix live view or any other idea which is better than mine :slight_smile: .

Most Liked

aseigo

aseigo

Assuming you are running this on a UNIX-like OS, if you just move the file to a new filename on the same volume, it won’t copy the data (which is slow) but simply create a new file entry for it (fast), and the Phoenix process will not delete it. I have a function that attempts to move the file, and only if that fails then falls back to copy for this exact purpose:

  @doc "Move a file, copy if we must"
  @spec move_file(src_path :: String.t(), dest_path :: String.t()) :: :ok | :error
  def move_file(src_path, dest_path) do
    # move if we can, copy if we have to
    case File.rename(src_path, dest_path) do
      {:error, :exdev} ->
        case File.copy(src_path, dest_path) do
          {:error, _} = error -> error
          _ -> :ok
        end

      {:error, _} = error -> error
      _ -> :ok
    end
  end
mcrumm

mcrumm

Phoenix Core Team

Correct, the uploaded temp file is cleaned up when its upload process exits, but this does not occur until you invoke one of the consume_uploaded_* functions for a given upload entry. So you can start uploading automatically and still wait to consume them in your form’s phx-submit handler.

Where Next?

Popular in Questions Top

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
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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

We're in Beta

About us Mission Statement