shahryarjb

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

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@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

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

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement