5

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 complains:

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 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.

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.

dodov
  • 5,206
  • 3
  • 34
  • 65
  • 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**](https://stackoverflow.com/questions/6587393/resource-interpreted-as-document-but-transferred-with-mime-type-application-zip) – Nope Jun 16 '17 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 accomplish in your web page? Are you just trying to trigger a file download based on a button push? – jfriend00 Jun 16 '17 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 Jun 16 '17 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 Jun 16 '17 at 15:40

2 Answers2

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"

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Nope... still does it. Although I was looking for a way to rename the downloaded file and I can - with this header. Thanks a lot! I still get this damn warning, though. – dodov Jun 16 '17 at 15:08
  • @HristiyanDodov take a look at [`res.download()`](http://expressjs.com/en/4x/api.html#res.download) as well for setting the download's filename – robertklep Jun 16 '17 at 15:32
0

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

Alunisiira
  • 21
  • 3