Displaying dynamically generated images in Yesod

亡梦爱人 提交于 2020-01-01 08:38:21

问题


I'm writing my first Yesod app. The application involves the user selecting to view a graph, dynamically generated based on data stored in a DB on the server. I know how to get the user request and create the image on the server's file system, but how do I create a response page presenting it?

P.S. As I'm using GnuPlot to generate the image, I only know how to write it as a file to the file system, but If you happen to know how to get the data in memory it'll probably be even better. Thanks,


回答1:


For a file on disk, you can use sendFile in your handler.

getImageR = do
    -- ... save image data to disk somewhere
    sendFile typeJpeg "/path/to/file.jpg"

For sending it from a ByteString in memory, use sendResponse.

getImageR = do
    bytes <- -- generate image data
    sendResponse (typePng, toContent bytes)

Make sure you specify the correct content type for your image.



来源:https://stackoverflow.com/questions/7147799/displaying-dynamically-generated-images-in-yesod

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!