2013-10-16 35 views
2

我需要下載存儲在數據庫中的文件。 我認爲快照有文件utils,它有助於文件上傳和下載,但它只處理駐留在文件系統上的文件。快照:下載存儲在數據庫中的文件

我被給予建議在單元IRC寫入BS功能將數據推送到瀏覽器。 此外,我被告知修改HTTP頭,以便瀏覽器將數據視爲文件並帶來保存/打開對話框。我今天玩了它,並有更多的問題。

我有這個至今:

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 

我怎樣才能做到這一點?

回答

3

您可以使用modifyResponsesetHeader(均來自Snap.Core)來設置響應的任意標題。像這樣:

modifyResponse $ setHeader "Content-disposition" "attachment; filename=document.pdf" 
相關問題