Phoenix LiveView + send_file

In plain Phoenix, one can offer a file-download with the Plug.Conn.send_file/5 function and it works nicely. Now, how would you solve this within a Phoenix LiveView context where there is no Plug.Conn available anymore, but a Phoenix.LiveView.Socket?

My solution so far is to make a GET request to an Endpoint where a Plug.Conn is handling the request. However, this takes me out of the LiveView context and causes a page reload. Is there a more interactive way of solving this with e.g. phx-click?

I have not used LiveView, but from my point of view, sending a file over the socket that is responsible to continously update the view doesn’t make any sense.

Of course you need a side channel to download the file, personally I’d just fall back to a regular HTTP request.

1 Like

What @NobbZ said and <a href="url" download>Download</a> should be enough to not make the browser move you to a new “page”. target="_blank" might also work.

6 Likes

Thanks, I solved it with the solution of @LostKobrakai and used an <a download...> tag for making the download call.