1

I have a Liveview with a form where the user makes a couple choices and then on submit the form shoots a post request over to a standard controller to handle the download. I have a case statement in the controller :download action that sends the download when it is created successfully, this works fine. I can't figure out how to keep the other :error case from redirecting away from the Liveview though.

case get_report do
  {:ok, csv} ->
    conn
    |> send_download({:binary, csv}, filename: "test.csv")
  {:error, _msg} ->
    do_something_pub_subby()
    conn
    |> ????
end
blackgreen
  • 34,072
  • 23
  • 111
  • 129
CherryPoppins
  • 59
  • 1
  • 7

1 Answers1

0

I had to do this a few months ago and I ended up redirecting to the LiveView route, like this:

case get_report do
  {:ok, csv} ->
    conn
    |> send_download({:binary, csv}, filename: "test.csv")
  {:error, _msg} ->
    do_something_pub_subby()
    conn
    |> put_flash(:error, ""unable to download)
    |> redirect(to: Routes.home_index_path(conn, :index))
end

It gets kind of ugly though. I found myself often assigning things to the conn to make this kind of thing work.

You should take a look at this library: https://github.com/karolsluszniak/phoenix_live_controller I haven't tried it myself but I'm pretty sure it supports what you're trying to do.

Peaceful James
  • 1,807
  • 1
  • 7
  • 16
  • That's what I ended up going with but I don't like how it does a full page reload even though it's not really noticeable to the average user. That for the link to the library, I'll check it out. – CherryPoppins Aug 12 '21 at 23:48