3

How to send an attachment as a response from an IHP action in such a way that after the file is sent, it is deleted from the server.

import System.IO (appendFile)

instance Controller MyController where
  action SendFileAction { myData } = do
    appendFile "data.txt" myData
  
  -- sendFile "data.txt" "text/plain"
  -- delete "data.txt"

Varun Rajput
  • 235
  • 1
  • 7

1 Answers1

3

There's no way to do this using IHP's renderFile. But you can work around this by reading the file into memory and then sending the response as a plain text. By setting the Content-Disposition header you can make it look like a normal file download in the browser.

import System.IO (appendFile)

instance Controller MyController where
  action SendFileAction { myData } = do
    appendFile "data.txt" myData
    content <- readFile "data.txt"
    deleteFile "data.txt"
    setHeader ("Content-Disposition", "attachment; filename=\"data.txt\"")
    renderPlain content
Marc Scholten
  • 1,351
  • 3
  • 5