最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct headers, but Chrome says "Resource interpreted as Document" - Stack Overflow

programmeradmin1浏览0评论

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);

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

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

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

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

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.

How do I make it not interpret 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, on the other hand, doesn't plain 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.

Edit:

Setting Content-Disposition doesn't work too:

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

I also use an <iframe> to initiate the request:

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.

Edit:

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

Edit:

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.

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);

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

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

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

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

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.

How do I make it not interpret 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, on the other hand, doesn't plain 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.

Edit:

Setting Content-Disposition doesn't work too:

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

I also use an <iframe> to initiate the request:

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.

Edit:

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

Edit:

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.

Share Improve this question edited Jun 16, 2017 at 15:29 dodov asked Jun 16, 2017 at 14:55 dodovdodov 5,8944 gold badges45 silver badges79 bronze badges 4
  • 1 This possible duplicate contains several different answers of which one could fit. Have a look at ► Resource interpreted as Document but transferred with MIME type application/zip – Nope Commented Jun 16, 2017 at 14:58
  • Well, setting the .src on an iframe tells the browser to EXPECT a web page, but you aren't sending a web page - thus the warning. That's the source of your issue. Please explain exactly what you're trying to acplish in your web page? Are you just trying to trigger a file download based on a button push? – jfriend00 Commented Jun 16, 2017 at 15:23
  • I'm trying to download a generated file from the server without page redirects. Also, I don't think the iframe is the problem. As I said in my edits, if I use a simple form with action attribute, I still get the warning. – dodov Commented Jun 16, 2017 at 15:28
  • @HristiyanDodov in general, location.href = '/path/to/download.zip' can be used to trigger a download without redirecting, provided that the server sets a Content-Disposition header. – robertklep Commented Jun 16, 2017 at 15:40
Add a ment  | 

2 Answers 2

Reset to default 2

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

Add the header: Content-Disposition: attachment; filename="filename.ext"

I got the same problem; I had a formular based post request which resulted in a file. Having the MIME Type and Content-Disposition set generated that very warning. I somewhere read, that new files should be openend in a new tab and the warning vanished. (But also could be because the new page was open and closing in no time ... so I couldn't verify if that warning appeared there)

Best, Jan

发布评论

评论列表(0)

  1. 暂无评论