HTTPoison AsyncResponse Failed - Network error with send_chunked

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

I used this code and It made no difference after 70mg it stoped and download was broken:

def long_download(conn, file_url, file_name) do
    file_infos = %{filename: "#{file_name}", url: "#{file_url}"}
    chunked_conn = prepare_chunked_conn(conn, file_infos.filename)
    file_infos.url
    |> stream
    |> Enum.reduce(chunked_conn, fn(chunk, conn) ->
      {:ok, new_conn} = chunk(conn, chunk)
      new_conn
    end)
  end


  defp prepare_chunked_conn(conn, filename) do
    content_type = MIME.from_path(filename)
    conn
    |> put_resp_content_type(content_type, "utf-8")
    |> put_resp_header("content-disposition", ~s(attachment; filename="#{filename}"))
    |> send_chunked(200)
  end


  def stream(url) do
    Stream.resource(fn -> build_stream(url) end, fn resp -> read_stream(resp) end, fn _resp ->
      :ok
    end)
  end

  defp build_stream(remote_file_url) do
    HTTPoison.get!(remote_file_url, [], stream_to: self(), async: :once)
  end

  defp read_stream(resp) do
    %HTTPoison.AsyncResponse{id: ref} = resp
    receive do
      %HTTPoison.AsyncStatus{id: ^ref} ->
        continue(resp)
      %HTTPoison.AsyncHeaders{id: ^ref} ->
        continue(resp)
      %HTTPoison.AsyncChunk{chunk: chunk, id: ^ref} ->
        _ = stream_next(resp)
        {[chunk], resp}
      %HTTPoison.AsyncEnd{id: ^ref} ->
        {:halt, resp}
    end
  end

  defp continue(resp) do
    resp
    |> stream_next
    |> read_stream
  end

  defp stream_next(resp) do
    {:ok, ^resp} = HTTPoison.stream_next(resp)
    resp
  end