OxygenFramework / Oxygen.jl

💨 A breath of fresh air for programming web apps in Julia
https://oxygenframework.github.io/Oxygen.jl/
MIT License
383 stars 25 forks source link

Returning a file #143

Closed Wikunia closed 5 months ago

Wikunia commented 5 months ago

Is there a way to return an image file for a route like /last_image which always returns the last image added in a folder?

Best I was able to do was to return a <img so far using the html function but I actually just want to return the image not as html.

ndortega commented 5 months ago

Hi @Wikunia,

In Oxygen, you can directly return the contents of a file in a route and everything works as expected. The file function used below is a helper function that's shorthand for read(open("myfile.png"), String). The Content-Type header is generated automatically based on the string's content.

If you know the type ahead of time and it remains constant then you'd be better off returning a HTTP.Response object with the correct headers

using Oxygen

get("/last_image") do
    # file content is directly returned
    file("myfile.png")
end

serve()
Wikunia commented 5 months ago

Perfect thanks!