在Yesod中显示动态生成的图像 [英] Displaying dynamically generated images in Yesod

查看:133
本文介绍了在Yesod中显示动态生成的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个Yesod应用程序。
应用程序涉及用户选择查看图形,该图形根据存储在服务器上的数据库中的数据动态生成。
我知道如何获取用户请求并在服务器的文件系统上创建映像,但是如何创建一个响应页面来显示它?

当我使用GnuPlot生成图像时,我只知道如何将它作为文件写入文件系统,但如果您碰巧知道如何获取内存中的数据,它可能会更好。
Thanks,

解决方案

对于磁盘上的文件,您可以使用 sendFile

  getImageR = do 
- ...将图像数据保存到某处的磁盘
sendFile typeJpeg/path/to/file.jpg

ByteString 在内存中,使用 sendResponse

  getImageR = do 
bytes< - - 生成图像数据
sendResponse(typePng,toContent bytes)

确保您指定正确的内容类型图片。


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,

解决方案

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.

这篇关于在Yesod中显示动态生成的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆