How can I send a chunked file to a user for download?

The Enum.reduce/3 way I’ve shown earlier should do it, as this is just the way as it’s done in the docs.

In elixir doc:

Enum.reduce([1, 2, 3], 0, fn x, acc -> x + acc end)

I think it needs to be changed |> Enum.reduce(conn, {content, _}, conn ->

file_streamed
    |> Enum.with_index
    |> Enum.reduce(conn, {content, _}, conn -> chunk(conn, content) end)

I have SyntaxError. I think something was not considered :thinking:

I change that to it

file_streamed
    |> Enum.with_index
    |> Enum.reduce(conn, fn {content, _}, conn ->
      chunk(conn, content)
    end)

but the pic is not valid, and I have

Request: GET /download
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Plug.Conn.chunk/2

Yeah, sorry, I missed the fn

I’m not sure though why it doesn’t work… Do you get more from the matcherror?

1 Like

no but I listed all my terminal error : https://gist.github.com/shahryarjb/3dde2cfadd845395bf47f31d2100bcba

Oh, chunk returns a tuple as it seems… I haven’t really looked at it… Just take the example using reduce_while from chunks docs and adjust it to your needs.

I can’t give a better code sample right now as I’m on mobile.

2 Likes