2013-10-16 2 views
2

데이터베이스에 저장된 파일을 다운로드해야합니다. 나는 snap이 파일 업로드와 다운로드에 도움이되는 파일 utils를 가지고 있다고 믿지만, 파일 시스템 상에 상주하는 파일들만 취급한다.Snap : 데이터베이스에 저장된 파일을 다운로드하십시오.

나는 데이터를 브라우저로 보내기 위해 스냅 IRC 기능에 대한 조언을 받았다. 또한 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

setHeader (모두 Snap.Core)과 조합하여 modifyResponse을 사용하여 응답의 임의 헤더를 설정할 수 있습니다. 좋아요 :

modifyResponse $ setHeader "Content-disposition" "attachment; filename=document.pdf"