Created
April 3, 2018 09:59
-
-
Save myobie/5050cf4e000d117744536c0e7815f118 to your computer and use it in GitHub Desktop.
Send a file in phoenix (plug)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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