[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







arrow
arrow

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