Hello, I have 2 different server, the one of them is ftp and the other my phoenix site. then when my user sends me a request which is a download a file, I used this code:
def download(conn, file_url, file_type, file_name, "stream") do
%HTTPoison.AsyncResponse{id: id} = HTTPoison.get!(file_url, %{}, stream_to: self())
conn = conn
|> put_resp_content_type("#{file_type}")
|> put_resp_header("Content-disposition","attachment; filename=\"#{file_name}#{Path.extname(file_url)}\"")
|> put_resp_header("Content-Type", "application/octet-stream")
|> send_chunked(200)
# image/event-stream
process_httpoison_chunks(conn, id)
end
and receive like a stream:
def process_httpoison_chunks(conn, id) do
receive do
%HTTPoison.AsyncStatus{id: ^id} ->
process_httpoison_chunks(conn, id)
%HTTPoison.AsyncHeaders{id: ^id, headers: %{"Connection" => "keep-alive"}} ->
process_httpoison_chunks(conn, id)
%HTTPoison.AsyncChunk{id: ^id, chunk: chunk_data} ->
conn |> chunk(chunk_data)
process_httpoison_chunks(conn, id)
%HTTPoison.AsyncEnd{id: ^id} ->
conn
end
end
but I have a big problem, my code doesn’t work for me when the file requested is more than 70mg. after downloading 70mg it shows me Failed - Network error
. and I think connection is closed.
Please help me to improve my code that keeps download connection until my user download the file successfully!
Thanks