shahryarjb
How can I send a chunked file to a user for download?
Hello, I created a post (https://forum.elixirforum.com/t/downloading-with-user-token) before and I need a Feature which helps me to send a file that be chunked.
Imagine I have big video and my user should download it, I need to chunk this file, mean this file should be cut to multi parts (chunk).
normal Phoenix download
def download(conn, _params) do
file = File.read!("/Users/test/name.png")
conn
|> put_resp_content_type("image/png")
|> put_resp_header("Content-disposition","attachment; filename=\"test.png\"")
|> put_resp_header("X-Accel-Redirect", "/tempfile/download/test.png")
|> put_resp_header("Content-Type", "application/octet-stream")
|> send_resp(200, file)
end
this is not a stream file, if I change the first line to this:
file = File.stream!("/Users/shahryar/Desktop/Khat-Ghalam.png", [], 204800)
I have this error:
no function clause matching in Plug.Conn.resp/3
if I use |> send_resp(200, file.path) it works sometimes . but I think it isn’t a stream file and I can’t create it many parts.
it should be noted, I wanted to use |> send_chunked(200) in my phoenix but I have error.
at least I need to create a download file that sends the file as section to section to my user
Thanks
Most Liked
hubertlepicki
Well, I don’t know what you mean precisely here or that you know what “chunked response” is in HTTP. Judging what you want to achieve, i.e. send video file to client / player, you most likely want to stream the file.
Sending file to client with send_file will, most likely, meet your requirements. It instructs Cowboy to send the file to client, and it will be done in efficient manner, reading parts of the file from disk and sending it to the client as it receives previous parts. This should be good enough to do basic video player for example.
This may not be good enough if you want to do some smart buffering, i.e. want to load just part of the file to buffer next 15s and then when user plays some more, buffer another 15s. You would have to do some sync with player and control the streaming process yourself, and yes in such case probably relying on send_file will not be enough. But for basic streaming it will be enough and efficient enough.
benwilson512
@shahryarjb as I understand it you don’t need to manually make it stream, it always streams. There is an example in the documentation: Plug.Conn — Plug v1.8.1
wanton7
There bug in Cowboy 2.x (no back pressure). That means chunked upload will use lot of memory, basically load file in to memory first. Very high memory usage when streaming file with Phoenix - #13 by wanton7
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









