文件名在正确设置content-disposition时变为undefined.zip

4

我正在使用httpServletResponse返回一个字节数组,并且content-disposition头已经正确设置。而且我获取到的内容也是正确的。但不知何故,文件名变成了undefined.zip。

以下是代码片段:

        // set content attributes for the response
        response.setContentType("application/octet-stream"); 
        response.setContentLength((int) packageZipFile.length);  
        // set headers for the response
        String headerKey = "Content-Disposition";
        String headerValue = "attachment; filename=\"abc.zip\"";
        response.setHeader(headerKey, headerValue);

        // get output stream of the response
        outStream = response.getOutputStream();
        outStream.write(packageZipFile);  

在浏览器的REST调用响应中,它被正确地设置为如下所示: Content-Disposition:attachment; filename="abc.zip"
请问我做错了什么?先谢谢您的帮助!
1个回答

1
我遇到了同样的问题,原来我正在访问localhost,并尝试从192.168.x.x下载文件。这种行为在chrome和firefox中都是一致的。
以下是我在node中设置头部的方法。
ctx.response.set('content-type', 'application/zip, application/octet-stream');
ctx.response.set('content-disposition', `attachment; filename="filename-here.zip"`);
ctx.response.set('filename', `filename-here.zip`);

希望这能帮助到任何在三年后遇到这个问题的人 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接