With Liveview, what is best way to download a file to the user's browser? (redirect through Phoenix Controller and back to Liveview, or use a background Task?)

Welcome to the forum @GeorgeMiller !

is there a reason why you couldn’t trigger the download with an ordinary link to a file in the priv/static folder?

  1. When your file is created (let’s call it data.csv), copy it to priv/static/. You can get the location of the priv folder using :code.priv_dir(:your_app_name).

  2. Create a download link in your view:

 <%= link "Download", to: Routes.static_path(@socket, "data.csv"), target: "_blank" %>

(You will have to add the csv extension to the list of accepted extensions in the configuration for Plug.Static in your endpoint for the download to work)

No need to write a custom controller and set response headers. The download will be handled by Plug.Static which will also set all the necessary headers.

4 Likes