我的控制器中有一个方法可以下载上传的文件。我试图将Content-Length标题添加到下载方法中,以便下载进度条。
问题是我的工作不起作用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()
}
}
答案 0 :(得分:2)
你必须设置它有一个Header参数:
CGPoint center = child.center;
center.x = parent.center.x;
child.center = center;
此外,您可能希望在流式传输文件后禁用视图呈现。
response.setHeader("Content-Length", "${bytes.length}")