wlminimal

wlminimal

How to display preview image using Plug.Upload

I am trying to let user upload image in the form, then when an user upload an image and submit the form, I do put_session %Plug.Upload{} in session data then forward to new page for preview.

  def preview_mms(conn, %{ "upload-image" => upload_image}) do
    conn
    |> put_session(:upload_image, file)
    |> redirect(to: image_preview_path(conn, :index))
  end

then in ImagePreviewController.ex

def index(conn, _params) do
  %{filename: filename, path: path} = get_session(conn, :upload_image)
  temp_file = Path.join(path, filename)
  render conn, "index.html", temp_file: temp_file
end 

in index.html.eex

<div class="row">
  <div class="col12">
    <img src="<%= @temp_file %>" alt="Upload Image">
  </div>
</div>

But got an error

(Phoenix.Router.NoRouteError) no route found for GET /tmp/plug-1528/multipart-1528402326-568658593711254-4/momo.jpg

I read docs in Plug.Upload and it says

“Uploaded files are stored in a temporary directory and removed from that directory after the process that requested the file dies.”

What does it mean? does it mean uploaded file in temp directory deleted after new request? even though I try to put that in session data?

What am I missing? Please help!

Most Liked

LostKobrakai

LostKobrakai

So move the file to a different location and it’s not going to be removed automatically. But you’re then also responsible to clean up at that new location in case people abandon the upload and such.

I always uploading files to S3 but into a temp. folder, which is set up to delete files after 24h – this way the cleanup is automated for me – and if the user saves the uploaded file I only move the file on S3 to a location without auto cleanup.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement