chris-menz

chris-menz

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!

Showing Posts 1 to 6

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 OP

The code doesnt even execute to this point. When I click the button, it starts executing the download function, but then stops and redirects back to the order page.

Heres the console output:

[info] GET /orders/1
[debug] Processing with SnapshotManagerWeb.OrderController.order_page/2
  Parameters: %{"id" => "1"}
  Pipelines: [:browser]
[debug] QUERY OK source="orders" db=5.6ms queue=1.1ms idle=281.8ms
SELECT o0."id", o0."sub_total", o0."notes", o0."user_id", o0."shipping_address", o0."billing_address", o0."order_items", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 WHERE (o0."id" = $1) [1]
[info] Sent 200 in 129ms
[info] GET /order-download/1
[debug] Processing with SnapshotManagerWeb.OrderController.download_order/2
  Parameters: %{"id" => "1"}
  Pipelines: [:browser]
[debug] QUERY OK source="orders" db=1.9ms idle=1894.1ms
SELECT o0."id", o0."sub_total", o0."notes", o0."user_id", o0."shipping_address", o0."billing_address", o0."order_items", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 WHERE (o0."id" = $1) [1]
[debug] Live reload: priv/static/order-1/album-2/3.png
[debug] Live reload: priv/static/order-1/album-2/4.png
[info] GET /orders/1
[debug] Processing with SnapshotManagerWeb.OrderController.order_page/2
  Parameters: %{"id" => "1"}
  Pipelines: [:browser]
[debug] QUERY OK source="orders" db=0.6ms idle=698.7ms
SELECT o0."id", o0."sub_total", o0."notes", o0."user_id", o0."shipping_address", o0."billing_address", o0."order_items", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 WHERE (o0."id" = $1) [1]
[info] Sent 200 in 2ms

You can see that there are two photos downloaded in this instance. There should be many more but you can see that redirect happen immediately which kills the downloading function. That redirect is not coming from the redirect function in the code though which is throwing me off. I can add a pry() or IO.puts directly after this line

path = ImageDownloader.download_images_for_order(String.to_integer(id))

and it doesnt get called. So something is happening to stop the execution of the controller function.

sodapopcan

sodapopcan

It looks like something is triggering Live Reload based on that error! The only think I can think of is that you are downloading to a location that is being watched by the reloader, like in priv/ perhaps?

When it does get figured out, you should still definitely change that send_download line! :slight_smile:

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.

chris-menz

chris-menz OP

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

LostKobrakai

LostKobrakai

— 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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews