gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:05:55 25 4
gpt4 key购买 nike

我的 Controller 中有一个方法可以下载上传的文件。我试图将Content-Length header 添加到下载方法中,以便下载进度条起作用。

问题是我无法正常工作,这是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()
}
}

最佳答案

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

response.setHeader("Content-Length", "${bytes.length}")

另外,在流式传输文件后,您可能想禁用 View 的呈现。
webRequest.renderView = false

关于grails - 在Grails中下载文件时如何设置内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com