chris-menz

chris-menz

Phoenix Controller returning before code execution completes, image download

Hello, I need to set up file download that happens dynamically at runtime. Essentially there are images stored on s3 that need to be downloaded, sorted into a folder and then sent to the clients browser as a zip file. The code for doing all this is set up and working properly. The problem I’m running into is the controller will return without waiting for all the images to download, it returns right away so only 2-5 get downloaded, for my use case there could be 100+ images that need to be downloaded. I’m not sure why the code is not running asynchronously.

If i remove the connection return at the end of the controller, then all images get downloaded as intended, but it throws an error as the controller must return a connection.

lib/router.ex

    get "/orders/:id", OrderController, :order_page
    get "/order-download/:id", OrderController, :download_order

lib/controller/order_controller.ex

  def order_page(conn, %{"id" => id}) do
    order = SnapshotManagement.get_order(id)

    render(conn, "order.html", order: order)
  end

  def download_order(conn, %{"id" => id}) do
## the image downloader function downloads all the images to a 
## folder in priv/static and returns the path to that folder
## this is working as intended so not including the code here
    path = ImageDownloader.download_images_for_order(String.to_integer(id))
    old_path = File.cwd!
    File.cd!(path)
    {:ok, filename} = :zip.create("order.zip", ['./'], [{:cwd, path}])
    File.cd!(old_path)

    send_download(conn, {:file, path <> "/" <> filename})

    File.rm_rf!(path)

    redirect(conn, to: "/order/" <> id)
  end

order.html.eex

<li class="list-group-item"><a href="/order-download/<%= @order.id %>" class="btn btn-primary btn-lg" tabindex="-1" role="button" aria-disabled="true">Download</a></li>

Any help or ideas for other setups are appreciated, thanks!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Where are you putting these files that you are downloading? If you’re putting them in priv/static then you’re triggering the live code reloader since you’re changing the files available. This immediately is triggering a refresh on the front end.

I would consider making a temporary directory (consider Briefly Usage Guide — briefly v0.5.1) and putting the files to send in that.

Also Liked

sodapopcan

sodapopcan

Try changing:

send_download(conn, {:file, path <> "/" <> filename})

to

conn = send_download(conn, {:file, path <> "/" <> filename})

…which should have the right headers on it (sorry, I did not test this). Remember, conn is an immutable struct!

chris-menz

chris-menz

That was it! thank you guys so much @benwilson512 @sodapopcan

Last Post!

LostKobrakai

LostKobrakai

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement