haskell - 快照 : Download files stored on a database

标签 haskell http-headers haskell-snap-framework

我需要下载存储在数据库中的文件。 我相信 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

我该怎么做?

最佳答案

您可以结合使用 modifyResponsesetHeader(均来自 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/

相关文章:

c# - 在 WP7 中发出 http post 请求发送 JSON 文件

authentication - 您如何在单个 POST 请求期间使用 Snap 的身份验证机制?

html - 在 HandsomeSoup 中,如何选择元素的内部 html?

haskell - 有没有办法直接引用 Haskell 中的类型类实例?

http-headers - 服务器上的 http 保持事件超时

python - 可以设置接受语言 header 但不能设置连接 header 吗? PhantomJS(带有 Python 的 Selenium Webdriver)

docker - 如何完全删除快照应用程序(docker)

forms - 使用 Digestive Functors 和 Snap 进行单元验证

reflection - 加载动态haskell模块

haskell - 为什么 Haskell 中的递归函数这么慢?