我需要下载存储在数据库中的文件。 我相信 snap 有文件实用程序,可以帮助文件上传和下载,但它只处理驻留在文件系统上的文件。
我得到了关于使用 snap IRC to writeBS 函数将数据推送到浏览器的建议。 另外,我被告知修改 HTTP header ,以便浏览器将数据视为文件并带来保存/打开对话框。我今天玩了一下,还有更多问题。
到目前为止我有这个:
getFirst :: AppHandler ()
getFirst = do
modifyResponse $ setContentType "application/octet-stream" -- modify HTTP header
result <- eitherWithDB $ fetch (select [] "files") -- get the file from db
let doc = either (const []) id result -- get the result out of either
fileName = at "name" doc -- get the name of the file
Binary fileData = at "blob" doc -- get the file data
writeBS fileData
您能告诉我这是否是正确的做法吗?
它可以工作,但缺少一些东西:
- 如何将文件名和文件类型传递给浏览器?
- 如何设置
Content-Disposition
?
所以我需要能够设置这样的东西:
Content-Disposition: attachment; filename=document.pdf
Content-Type: application/pdf
我该怎么做?
最佳答案
您可以结合使用 modifyResponse
和 setHeader
(均来自 Snap.Core
)来设置响应的任意 header 。像这样:
modifyResponse $ setHeader "Content-disposition" "attachment; filename=document.pdf"
关于haskell - 快照 : Download files stored on a database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19408274/