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

Showing Posts 1 to 9

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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews