我看了很多这样的问题,但我真的想不通......
我使用 archiver和 express Node.js 模块。我只想向客户端发送一个 zip 文件。我的代码大致如下所示:
res.set("Content-Type", "application/zip");
archive = archiver("zip", {store: true});
archive.pipe(res);
当请求时,zip 文件被正确传输,但 Chrome 提示:
Resource interpreted as Document but transferred with MIME type application/zip
如果我做了一些愚蠢的事情,比如将内容类型设置为 text/json
,我得到:
Resource interpreted as Document but transferred with MIME type text/json
很明显,我在 Node.js 中设置的内容正在发挥作用,而其他问题就是问题所在。它表示资源以 MIME 类型 applicatioin/zip 传输,但解释为文档。
如何让它不被解释为文档?
此外,我尝试了这些设置内容类型的方法:
res.type("application/zip");
res.contentType("application/zip");
但我得到了相同的结果。
另一方面,Mozilla 不会提示任何事情。一些类似问题的答案说这个警告可以忽略。我也应该忽略它吗?问题可能是存档直接通过管道传输到响应对象吗?尽管(我再说一遍)zip 已成功发送。
编辑:
设置Content-Disposition
也不起作用:
res.set({
"Content-Type": "application/zip",
"Content-Disposition": "attachment; filename='images.zip'"
});
我还使用 <iframe>
发起请求:
document.getElementById("iframe-downloader").src = requestUrl;
原因是因为我不想页面重定向。我唯一能做到的就是这样。
编辑:
如何更改我的 Chrome 设置 this answer指出也不起作用。
编辑:
This answer说是因为 <iframe>
.如果我通过一个简单的 <form>
发出请求与 action="/archive"
, 它仍然不起作用。
最佳答案
我认为这意味着 Chrome 正在尝试将文件显示为网页,这不是您想要的。
添加 header :Content-Disposition: attachment;文件名="文件名.ext"
关于javascript - header 正确,但 Chrome 显示 "Resource interpreted as Document",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592322/