GeorgeMiller

GeorgeMiller

I am coding my first Liveview application. Liveview is awesome, but I have been stumped on something for several days now and need some help.

My Liveview application needs to download a file to the client’s browser. My Liveview page renders a button for doing the download.

Summary of behaviour that I see:
a) The controls (dropboxes, buttons) on the Liveview page work fine
b) When the Download button is pressed, the file is downloaded. The displayed page is still the Liveview page.
c) At this point, the Liveview code stops working. The controls no longer work (including the Download button).

Liveview supports uploads out-of-the-box, but does not support downloads. I found two threads (one and two) which say that in order to download a file, it is recommended to redirect to a
Phoenix Controller.

My Liveview application redirects to a Phoenix controller, which in turn downloads the file. This part works fine. There are several options for downloading files to the client browser (Phoenix.Controller.send_download, Plug.Conn.send_file, or Plug.Conn.chunk). All of these options make use of the ‘Conn’ object.

My Liveview code that handles the download button event:

@impl true
def handle_event("download-btn-event", _, socket) do
    {:noreply, socket |> redirect(to: "/api/download")}
end

And an exerpt of router.ex:

scope "/api", MyApplicationWeb.Api, as: :api do
    pipe_through :api
    get "/download", DownloadController, :download
end

And the Phoenix Controller code is defined like this:

defmodule MyApplicationWeb.Api.DownloadController do
    use MyApplicationWeb, :controller

    @filename "Downloaded.csv"

    def download(conn, params) do
        conn =
            conn
            |> put_resp_content_type("text/csv")
            |> put_resp_header("content-disposition", ~s[attachment; filename="#{@filename}"])
            |> ...
            |> Phoenix.Controller.send_download({:file, path})
    #     |> Phoenix.Controller.redirect(to: "/myapplication")
    #     |> halt

Inside this Phoenix controller action I have tried returning Conn, or redirecting the connection back to liveview (to use my router.ex to bring the user back to Liveview page), or halting the Conn process. The commented out stuff are ideas I was trying. In particular, the redirect idea gives an error like this:

** (exit) an exception was raised:
** (Plug.Conn.AlreadySentError) the response was already sent

I found this excerpt at Plug.Conn — Plug v1.20.2 :

The connection state is used to track the connection lifecycle. It starts as :unset but is changed to :set (via resp/3) or :set_chunked (used only for before_send callbacks by send_chunked/2) or :file (when invoked via send_file/3). Its final result is :sent, :file or :chunked depending on the response model.

I think the above shown error is because the Conn was already used to send the file, and so it does not allow the Conn to be re-used for another purpose (like redirecting to another page) ?

Once the file has been downloaded, I want the user to continue interacting with the Liveview.

An alternate idea I has was to spawn a separate process for doing the download (for example: following advice like Setup a supervised background task in Phoenix or this thread on handling background jobs with elixir/phoenix. And sure enough I am able to spawn a background process, but that process does not have a ‘Conn’ object so it can’t do the file download.

So, I’m stuck.

Questions:

  1. How to redirect from Liveview to Phoenix, get the file downloaded to the user, and then redirect back to Liveview again (need to re-use the Conn object after the file download) ?
    Alternatively, how to launch a background task that has the ‘Conn’ so it can carry out a file download asynchronously?
  2. Assuming the redirect approach, does it matter if I redirect to a Phoenix controller that is setup via pipe_through :api or :browser in router.ex?
  3. (Bonus points) Is there a way to be notified when the file has been downloaded? I’ve like to put a flash message on the screen, if possible. With the background process, I was thinking Async.await() could be used. For the send_download from Controller approach, I have no idea how to know when the download has finished.

Thanks for reading!

Showing Posts 1 to 10

trisolaran

trisolaran

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.

GeorgeMiller

GeorgeMiller OP

Hi @trisolaran,

Thank you for the well-thought out response. I like what you are saying, and those instructions are clear.

I should add specifications for the problem I am solving. The application stores data in a database and the Liveview webpage displays a table of this data. The other webpage controls (input fields, dropboxes) are filtering/updating the data being displayed. And then the Download button dynamically creates the data file for download, considering the settings of all the filters.

Given these specifications (sorry, I should have given them in the original post), I do not have a path to the file at the time that the page is rendered. The file is generated dynamically after the page is rendered.

The data file can be quite large, which is the reason there are filters.

Having said that, I guess one idea is to setup a download link as you suggest, and then dynamically re-write the contents of the file (in priv/static) each time a filter is applied. The issue would be that perhaps the download file is being re-written at the time that the download button is pressed?

trisolaran

trisolaran

Hi,

Thanks, the problem is clearer now. Some quick comments:

And then the Download button dynamically creates the data file for download, considering the settings of all the filters.

When you dynamically create the file based on the applied filters, you could create it in the /priv/static folder directly

and then dynamically re-write the contents of the file (in priv/static) each time a filter is applied

I think this would be rather inefficient and slow. You can update the view whenever the filter is applied, and when the user is sure that the data they see is what they want to download, they could hit the download button. Only then do you need to create the file. You don’t need to re-create the file whenever a filter is applied (if I understood the application correctly).

Your application could then render the download link after the the file creation is completed, and the user can click it to download the file.

Something like: user applies filters to tableusers hits download buttonfile is createddownload link is rendereduser clicks the download link

This would be quite easy and work well AFAICS.

But what you’d like to have is slightly different:

user applies filters to tableuser hits download buttonfile is createdfrontend downloads the file

So everything done with a single user interaction.

I’m not sure, but my feeling is that if you want to achieve this you are gonna have to write some Javascript. Just a random idea: you could render an invisible download link and have a liveview client hook (JavaScript interoperability — Phoenix LiveView v1.2.5) that uses axios or similar library to download the file when the link is rendered, without involving the user. This might be a viable option, but I haven’t thought it through.

derek-zhou

derek-zhou

If your dynamically generated data is small and you don’t care about some overhead, you can just push_event to the client side. You will have to base64 encode the data so it can pass in json. and you may have to chunk it; however, you don’t have to leave the comfort of your liveview.

ppiechota

ppiechota

Hi @GeorgeMiller. Have you managed to resolve this issue? I have exactly the same case and don’t know how to properly download a file with “redirecting to controller” scenario. I’m able to download a file but my LV hangs after redirect.

LV:

  @impl true
  def handle_event(
        "generate_excel_report",
        _,
        %{assigns: %{report: report}} = socket
      ) do
    Cachex.put(:cache, "report", report)
    {:noreply, redirect(socket, to: Routes.excel_path(socket, :index))}
  end

Controller:

defmodule FlipWeb.ExcelController do
  use FlipWeb, :controller

  def index(conn, _params) do
    {:ok, report} = Cachex.get(:cache, "report")

    conn
    |> put_resp_content_type("text/xlsx")
    |> put_resp_header("content-disposition", "attachment; filename=report.xlsx")
    |> render("report.xlsx", %{report: report})
  end
end
evadne

evadne

Sorry, old code follows, this has worked for 10+ years:

jQuery('<iframe>').attr('src', file.url).hide().appendTo(jQuery(document.body))

So let’s adapt it to modern JS / LV interop with this in the body:

window.addEventListener(`phx:download`, (event) => {
  let uri = event.uri;
  let frame = document.createElement("iframe");
  frame.setAttribute("src", response.uri);
  frame.style.visibility = 'hidden';
  frame.style.display = 'none';
  document.body.appendChild(frame);
});

Then from the LiveView you use push_event at the appropriate juncture (name of event, would be download in this case, and it would have the URI) — win/win solution, no redirection (so you keep the LV running), the download proceeds via the same authentication system you have used (as it will go through a known set of Plugs), and the file is downloaded!! Plus this would work nice with things you have to stream (such as via Packmatic etc)

Would recommend reading JavaScript interoperability — Phoenix LiveView v1.2.5 thoroughly.

PS: Could also use a phx-ignore container to hang your iframe in, or just make some space for it in your layout where LV would not touch, all depending on how your app is set up

PS 2: Redirecting away from a LV to something with content-disposition that is not inline probably causes what @ppiechota is seeing. So avoiding full redirection (you can’t get back easily, anyway) or anything that would cause the LV to stop running, would be key here.

12
Post #6
abbyjones

abbyjones

@GeorgeMiller - thanks for asking this question and detailing your requirements. I’m working on a feature that has the same requirements, so this thread has been golden.

Using @trisolaran’s suggestion of the Routes.static_path helper along with @evadne’s sample JavaScript code is working beautifully with a sample csv file.

Another requirement we might have in common is that the generated files are meant to be temporary and don’t need to be saved on the server after they’ve been downloaded. Curious if anyone has a strategy they like for deleting the files after download, or after some period of time?

evadne

evadne

Use a dedicated S3 bucket with lifecycle policy to remove objects after X days.

mcrumm

mcrumm

Phoenix Core Team

@abbyjones @ppiechota @GeorgeMiller The server-side redirect is definitely the issue. If you redirect on the server, then the LiveView process shuts down. However since the download redirect does not change the browser document’s location, the (now disconnected) LiveView page is still accessible to the end-user.

I like @evadne’s solution if you want to force the download to start automatically. Generally I prefer to just render a download link. If the response sends content-disposition: attachment;... then everything else Just Works™.

derek-zhou

derek-zhou

In Liv every mail attachment is downloaded in-band, through the same Liveview websocket, so I don’t need to worry about liveview disconnect, temp file, or S3 buckets. Everything download automatically into blob urls in the web page. The caveat is that you can’t do very large files. A few files, each a few megabytes are no trouble at all.

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

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
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews