Grails File Download

前端 未结 1 1380
渐次进展
渐次进展 2020-12-08 02:41

I\'m trying to craete a site which allows users to upload any file type they like. I\'ve implemented this feature fine, and the file is held on the server. Later on they can

相关标签:
1条回答
  • 2020-12-08 03:43

    The problem is that you read the content of the file into a String by using "file.text". The content of the file is converted with the system character encoding even if the content is binary, not text (eg. PDF files are binary) and sent to the client using the response encoding and thereby modifing the binary content. You should rather use a different approach like this:

    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
    
    0 讨论(0)
提交回复
热议问题