10

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,

Uri Barenholz
  • 693
  • 3
  • 13

1 Answers1

10

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.

user316146
  • 1,307
  • 1
  • 12
  • 19
hammar
  • 138,522
  • 17
  • 304
  • 385
  • 2
    I actually think you could just use "return" instead of sendResponse in this case, since you don't need any short-circuiting. Also, remember to use liftIO if you're using normal IO functions. – Michael Snoyman Aug 22 '11 at 16:19