pavancse17

pavancse17

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: .

First 9 of 9 Posts Switch mode

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
pavancse17

pavancse17 OP

Thanks @aseigo seems to be good idea. will try it out .

pavancse17

pavancse17 OP

On the same notes. how are you handing destination folder location ending up with unused files?
Consider a scenario like user uploads file and without submitting it, user will re-upload another file. In my case I am tracking 1st file path in socket assigns and deleting 1st file before copying 2nd file and updating path with 2nd file in assigns and story continues till user submits :slight_smile: .

aseigo

aseigo

I don’t have that particular issue as the processing begins as soon as the upload occurs. It isn’t tied to a form submission in my case …

pavancse17

pavancse17 OP

hm got it :+1:

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.

mcrumm

mcrumm

Phoenix Core Team

Can you share more details about this? Specifically I am curious about two things:

  1. What kinds of file validation are you performing? Are you inspecting the actual file bytes?

  2. You are uploading to your web server to turn around and transfer the file to S3, have you considered migrating to client-side uploads via pre-signed URLs?

pavancse17

pavancse17 OP

Yes I actually want to read the file and match it to few database data & inform the user if any invalid data
present before submitting itself.

pavancse17

pavancse17 OP

Yes I am aware of this. But I don’t want to save some invalid file in S3. BTW I can remove it from S3 if the file is invalid. but tricky part is when the file is valid and user leaves page without submitting the form.

All these things I can avoid by removing auto_upload. But my business folks liked it a lot the way it shows the errors instantly. So don’t want to disappoint them :slightly_smiling_face:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement