Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Grails中下载文件时如何设置内容长度_Grails - Fatal编程技术网

在Grails中下载文件时如何设置内容长度

在Grails中下载文件时如何设置内容长度,grails,Grails,我的控制器中有一个方法,允许下载上传的文件。我正在尝试将内容长度标题添加到下载方法中,以便下载进度条正常工作 问题是我的工作不正常,response.setContentLength(${documentInstance.fileSize}”) 找不到该文件时出错。如果它取消了这个方法,下载就可以了 这里是方法 def download(long id) { Document documentInstance = Document.get(id) if ( documentIns

我的控制器中有一个方法,允许下载上传的文件。我正在尝试将内容长度标题添加到下载方法中,以便下载进度条正常工作

问题是我的工作不正常,
response.setContentLength(${documentInstance.fileSize}”)

找不到该文件时出错。如果它取消了这个方法,下载就可以了

这里是方法

def download(long id) {
    Document documentInstance = Document.get(id)
    if ( documentInstance == null) {
        flash.message = "Document not found."
        redirect (action:'list')
    } else {
        response.setContentType("APPLICATION/OCTET-STREAM")
        response.setHeader("Content-Disposition", "Attachment;Filename=\"${documentInstance.filename}\"")
        response.setContentLength("${documentInstance.fileSize}")
        def file = new File(documentInstance.fullPath)
        def fileInputStream = new FileInputStream(file)
        def outputStream = response.getOutputStream()
        byte[] buffer = new byte[4096];
        int len;
        while ((len = fileInputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush()
        outputStream.close()
        fileInputStream.close()
    }
}

您必须将其设置为具有标题参数:

response.setHeader("Content-Length", "${bytes.length}")
此外,您可能希望在流化文件后禁用视图的渲染

webRequest.renderView = false