How to send binary file?

I’m trying to proxy an image. So far I get cannot send_file/5 with null byte. I am not storing the file though, i just want to proxy the bitstring data

{content_type, image} = case resp do
  {:ok, %MachineGun.Response{status_code: status_code, body: body, headers: headers}} when status_code < 300 ->
    # TODO: ========================================================
    content_type = "image/png"
    {content_type, body}
  _ ->
    {nil, nil} # TODO: FIGURE SOMETHING OUT
end

case {content_type, image} do
  {content_type, image} when is_binary(content_type) and is_bitstring(image) ->
    conn
    |> put_resp_content_type(content_type)
    |> send_file(200, image)
  _ ->
    send_resp(conn, :ok, "")
end

What is the value of image at this point? A filepath should not have a null.

Its binary data, it turns out i could just do this: |> send_resp(:ok, image_data)

Ah yes, if it’s the file “contents” you send_resp, send_file is to send the file itself. :slight_smile:

1 Like