Load image binary from the database to render

Hi Guys,

I’m new to elixir and Phoenix. but im a bit stuck in this implementation. i am saving an image into the postgres DB as binary (i am intentionally not saving the image into a directory and retrieving the path). i am able to retrieve the image back from the db but how can i render the image back to the browser as if the image was visited by the browser like “http://abc.com/images/image1.png

i assume i might need to set the response header as “content-type”: “image/jpeg” and find a way to send back the image to the browser as if i was opening an image link

please how can i achieve this?

1 Like

So what you want to do in phoenix is render the image back right? So it’ll basically be a direct render?

If you store the mimetype this’ll make things a lot simpler.

Basically you’ll want to do this in your controller:

mime_type = "image/jpeg" # This should be fetched from db, or somehow from the binary.
image = "mybinaryimage" # Image binary grabbed from the database

conn
|> put_resp_content_type(image)
|> send_resp(200, image)
4 Likes

much easier than i expected actually. and yes i already have the mime_type gotten from Plug.Upload and stored in the DB. and i assume you meant |> put_resp_content_type(mime_type) . thanks