程式如下:
def file = new File(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 = new File(params.fileDir)
def fileName = 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
沒有留言:
張貼留言