Download file attachment Liveview

Hello, I am converting a page to liveview; in my controller I have this

          conn
          |> put_resp_header("content-disposition", "attachment; filename=#{file_name}")
          |> send_resp(200, content)

I attempted replacing conn with socket but that did not work. Now I understand websockets do not have headers. Does anyone have any ideas for the best approach to this problem?

The solution I’ve come with so far is to hit a Phoenix /export route (non-liveview). The only issue is that when I embed the eex template into my liveview, conn is not available for eex template. To solve that I just passed in the socket to replace conn.

My issue now is this:

invalid CSRF (Cross Site Request Forgery) token, please make sure that:

I have tried passing down the csrf_token of the liveview parent down to the child every which way possible. Nothing works. Is there something I don’t know?

Not using liveview to do file downloads. Have a link (can be within a liveview page) pointing to a normal controller managed route and return the requested file from there like you seem to already be doing.

1 Like

Yep! Update my original post to reflect my current situation. CSRF token troubles :confused:

There is a new talk about file uploading with Liveview…

2 Likes

Have you followed the steps here in regards to setting up your LiveView session and the csrf meta tag? If you do so, then csrf tokens should just work.

2 Likes