标头正确,但Chrome会显示“资源解释为文档" [英] Correct headers, but Chrome says "Resource interpreted as Document"

查看:110
本文介绍了标头正确,但Chrome会显示“资源解释为文档"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多类似这样的问题,但我真的不明白...

I read a lot of questions like this one, but I really can't figure it out...

我使用存档器

I use the archiver and express Node.js modules. I want to simply send a zip file to the client. My code looks roughly like this:

res.set("Content-Type", "application/zip");
archive = archiver("zip", {store: true});
archive.pipe(res);

根据要求,压缩文件已正确传输,但Chrome抱怨:

When requested, the zip file is transferred correctly, but Chrome complains:

资源被解释为文档,但以MIME类型application/zip传输

Resource interpreted as Document but transferred with MIME type application/zip

如果我做一些愚蠢的事情,例如将内容类型设置为text/json,我会得到:

If I do something stupid like setting the content type to text/json, I get:

资源被解释为文档,但以MIME类型text/json传输

Resource interpreted as Document but transferred with MIME type text/json

因此,显然我在Node.js中设置的工作正在完成,而问题出在其他方面.它说资源是以MIME类型applicatioin/zip 传输的,但是解释为为文档.

So apparently what I'm setting in Node.js is doing its job and something else is the problem. It says the resource was transferred with MIME type applicatioin/zip, but interpreted as Document.

如何使它不解释为文档?

此外,我尝试了以下几种设置内容类型的方法:

Also, I tried these ways of setting the content type:

res.type("application/zip");
res.contentType("application/zip");

但是我得到了相同的结果.

But I got the same result.

另一方面,Mozilla不抱怨任何事情.对类似问题的一些回答说,可以忽略此警告.我也应该忽略它吗?问题可能是归档文件直接传递到响应对象吗?尽管(我重复一遍),但是zip已成功发送 .

Mozilla, on the other hand, doesn't complain about anything. Some answers to similar questions said this warning can be ignored. Should I ignore it too? Could the problem be that the archive is piping directly to the response object? Although (I repeat), the zip is sent successfully.

设置Content-Disposition也不起作用:

res.set({
    "Content-Type": "application/zip",
    "Content-Disposition": "attachment; filename='images.zip'"
});

我还使用<iframe>发起请求:

document.getElementById("iframe-downloader").src = requestUrl;

原因是因为我不希望页面重定向.我唯一能做到的方法就是这种方式.

The reason is because I don't want page redirects. The only way I could manage to do it was this way.

更改我的Chrome设置,此答案指出的方法也不起作用.

Changing my Chrome settings how this answer points out doesn't work as well.

此答案说是因为<iframe>.如果我通过带有action="/archive"的简单<form>发出请求,该请求仍然无效.

This answer says it's because of the <iframe>. If I make the request through a simple <form> with action="/archive", it still doesn't work.

推荐答案

我认为这意味着Chrome浏览器试图将文件显示为网页,而不是您想要的.

I think that means Chrome is trying to display the file as a webpage, which is not what you wanted.

添加标题:Content-Disposition: attachment; filename="filename.ext"

这篇关于标头正确,但Chrome会显示“资源解释为文档"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆