Download or Export File from Phoenix 1.7 LiveView

Did you actuall add the Hooks to the liveSocket?

...
let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks})

A hook is the way to go. You can also add a hook to any node in the DOM and use this.handleEvent("phx:download-file", (event) => {}) in the hook to handle the pushEvent from the server.

The problem with including a script tag in the template is that it only works if the page included in the initial dead render. If you have live navigation and navigate to this page from another one, the script won’t be executed.

1 Like