IHP - How to send an attachment as a response from IHP Action?

72 Views Asked by At

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"

1

There are 1 best solutions below

0
Marc Scholten On BEST ANSWER

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