Skip to content

Instantly share code, notes, and snippets.

@myobie
Created April 3, 2018 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/5050cf4e000d117744536c0e7815f118 to your computer and use it in GitHub Desktop.
Save myobie/5050cf4e000d117744536c0e7815f118 to your computer and use it in GitHub Desktop.
Send a file in phoenix (plug)
defmodule AppWeb.Controller do
use AppWeb, :controller
# sending bytes
def show(conn, %{"id" => id}) do
data = Storage.get_data_for_file_id(id)
conn
|> put_resp_header(
"content-disposition",
~s(attachment; filename="#{id}.jpg")
)
|> put_resp_header("content-type", "image/jpeg")
|> resp(:ok, data)
end
# sending a file
def show(conn, %{"id" => id}) do
conn
|> put_resp_header(
"content-disposition",
~s(attachment; filename="filename-to-show-to-browser.jpg")
)
|> put_resp_header("content-type", "image/jpeg")
|> send_file(:ok, "path-to-file-on-disk.jpg")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment