[Grails](groovy)--如何實作將下載頁面程式及解決下載檔案名稱亂碼問題 程式如下:

def file =newFile(params.fileDir)    
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition","attachment;filename=${file.getName()}")

response.outputStream << file.newInputStream()// Performing a binary stream copy

如果你使用Firefox browser ,你的下載程式如果有檔案名稱為中文及檔案名稱中有空白,會不正常。 改程式如下:

def file =newFile(params.fileDir)  
deffileName=file.getName()
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition","attachment;filename=\""+ 
new String( fileName.getBytes("big5"), "ISO8859-1" )+ "\"")
response.outputStream << file.newInputStream()// Performing a binary stream copy

參考資料: Grails File Download Groovy Grails, How do you stream or buffer a large file in a Controller's response grails sending-a file to the browser

 
文章標籤
全站熱搜
創作者介紹
創作者 PG Levin Li 的頭像
PG Levin Li

程式開發學習之路

PG Levin Li 發表在 痞客邦 留言(0) 人氣(111)