We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Sending a file to the user
almirsarajcic
Created
Sending a file to the user is easy. Use the send_download/3
function in your Phoenix controller action like this.
# Sending a file from the filesystem
def download(conn, _params) do
path =
:my_app
|> :code.priv_dir()
|> Path.join("file_in_priv_dir.txt")
send_download(conn, {:file, path}, filename: "hello.txt")
end
# Sending a file from memory
def download(conn, _params) do
send_download(conn, {:binary, "hello world"}, filename: "hello.txt")
end
Here I pass the :filename
option to set the name under which the file will be saved on the user’s machine.
You can find other options here:
https://hexdocs.pm/phoenix/Phoenix.Controller.html#send_download/3
Copy link
copied to clipboard