ChatGPT解决这个技术问题 Extra ChatGPT

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

With Chrome 12.0.742.112, if I redirect with the following headers:

HTTP/1.1 302 Found 
Location: http://0.0.0.0:3000/files/download.zip
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
X-Ua-Compatible: IE=Edge
X-Runtime: 0.157964
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Tue, 05 Jul 2011 18:42:25 GMT
Connection: Keep-Alive

Which if followed returns the following header:

HTTP/1.1 200 OK 
Last-Modified: Tue, 05 Jul 2011 18:18:30 GMT
Content-Type: application/zip
Content-Length: 150014
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Tue, 05 Jul 2011 18:44:47 GMT
Connection: Keep-Alive

Chrome will not redirect, nor change the previous page, it'll just report the following warning in the console:

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

The process works correctly in Firefox, and also works fine in Chrome if I open a new tab and go directly to http://0.0.0.0:3000/files/download.zip. Am I doing something wrong, or is this a bug/quirk of Chrome?


R
Roy Hyunjin Han

You can specify the HTML5 download attribute in your tag.

<a href="http://example.com/archive.zip" download>Export</a>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download


"download" attribute is not support in ie or some other browser. if you have a other options please help me
That fixes Chrome, but breaks other browsers. My Android phones won't download from that kind of link.
sigh. THIS 'FIX' DOES NOT WORK FOR MOST BROWSERS - only Chrome. And it's 2016, so don't expect it to anytime soon either.
Doesn't make a difference in Chrome 2019
@all It's 2035 (I've come from future) and there is no computer to support this.
S
Saeed Neamati

In your request header, you have sent Content-Type: text/html which means that you'd like to interpret the response as HTML. Now if even server send you PDF files, your browser tries to understand it as HTML. That's the problem. I'm searching to see what the reason could be. :)


I've tried sending it as Content-Type: application/zip with no avail, it still tries to process it as a 'Document'. It's probably also worth pointing out that the zip URL is dynamic in my app, so it's nothing to do with caching.
Thanks! Could it be something to do with Chrome sending the Accept request header as text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, perhaps? I'm absolutely stumped here, I really am!
No, in text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 there is a part which says that Chrome accepts almost everything (*/*).
What are you talking about? There is no request header in the question. Both of them are response headers.
And I am sitting here wondering why this should be an answer but not a comment ?
2
2540625

I could not find anywhere just an explanation of the message by itself. Here is my interpretation.

As far as I understand, Chrome was expecting some material it could possibly display (a document), but it obtained something it could not display (or something it was told not to display).

This is both a question of how the document was declared at the HTML page level in href (see the download attribute in Roy's message) and how it is declared within the server's answer by the means of HTTP headers (in particular Content-Disposition). This is a question of contract, as opposed to hope and expectation.

To continue on Evan's way, I've experienced that:

Content-type: application/pdf
Content-disposition: attachment; filename=some.pdf

is just inconsistent with:

<a href='some.pdf'>

Chrome will cry Resource interpreted as document but transferred…

Actually, the attachment disposition just means this: the browser shall not interpret the link, but rather store it somewhere for other—hidden—purposes. Here above, either download is missing beside href, or Content-disposition must be removed from the headers. It depends on whether we want the browser to render the document or not.

Hope this helps.


E
Evan

I experienced this problem when serving up a PDF file (MIME type application/pdf) and solved it by setting the Content-Disposition header, e.g.:

Content-Disposition: attachment; filename=foo.pdf

Hope that helps.


but where to write this ?
@coder You add headers like this from your webserver. Not sure what you are using for that, so it's hard to give more info. What language or framework are you using for your server?
Have this in our headers but Chrome still throws the warning
nopes it does not fix anything on chrome Version 76.0.3809.132 (Official Build) (64-bit) i already have the headers attached
A
Ashley Williams

I've fixed this…by simply opening a new tab.

Why it wasn't working I'm not entirely sure, but it could have something to do with how Chrome deals with multiple downloads on a page, perhaps it thought they were spam and just ignored them.


What do you mean you "opened a new tab"??? You manually opened a new browser window and pasted the download URL in there?
Yes, exactly. I assumed it was a bug in the application-state of the particular tab.
I wouldn't expect the user to open a new tab... And I don't find opening one to download a file elegant either.
@Joram et. al. - opening a tab for a download (using target '_blank') - does not solve the problem, it just transfers the "Resource intrepreted as Document" console warning message to a new tab. Sweeping under the rug is not s solution.
This clearly does not solve the original answer. Seriously... What would I put in the user doc "copy the url, open a new tab, paste the url, ..."? Business down.
E
Elliveny

I had a similar issue when performing a file download through Javascript. Adding the download attribute made no difference but adding target='_blank' did - I no longer get the 'Resource interpreted as Document...' console message.

Here's my nicely simple code:

var link = document.createElement('a');
link.target = '_blank';
link.href = url;
document.body.appendChild(link); // Required for Firefox
link.click();
link.remove(); 

I haven't tried it with direct HTML but would expect it to work.

Note I discovered that Firefox requires the link to be appended to the document whereas Chrome will work without it.


just a warning, a target = '_blank' will trigger IE 11's popup prevention mechanism...
FYI, I did test it with direct HTML in Chrome (Version 87.0.4280.141, 64-bit) and it did not work.
J
JohnC

I encountered this same issue today with Chrome Version 30.0.1599.66 with my node.js / express.js application.

The headers are correct, express sets them properly automatically, it works in other browsers as indicated, putting html 5 'download' attribute does not resolve, what did resolve it is going into the chrome advanced settings and checking the box "Ask where to save each file before downloading".

After that there was no "Resource interpreted as document...." error reported as in the title of this issue so it appears that our server code is correct, it's Chrome that is incorrectly reporting that error in the console when it's set to save files to a location automatically.


C
Carmela

I encountered this when I assigned src="image_url" in an iframe. It seems that iframe interprets it as a document but it is not. That's why it displays a warning.


could you please say me how did you fixed it? as i m facing the same problem here when using an iframe
I used form data: var photoData = new FormData(); and then set the property contentType: false in my ajax request. The post request will be: Content-Disposition: form-data; And the content-type Content-Type: text/html
A
AS Mackay

I solved the problem by adding target="_blank" to the link. With this, chrome opens a new tab and loads the PDF without warning even in responsive mode.


I did this and it worked: window.open(href, '_blank'); and the new tab auto closes after the download.
u
user6096790

Just ran into this and none of the other information I could find helped: it was a stupid error: I was sending output to the browser before starting the file download. Surprisingly, I found no helpful errors found (like "headers already sent" etc.). Hopefully, this saves someone else some grief!


R
R. Salisbury

I had this issue in an ASP web site project. Adding a "Content-Length" header caused downloads to start working again in Chrome.


A
Asif Nowaj

This issue was re-appeared at Chrome 61 version. But it seems it is fixed at Chrome 62.

I have a RewriteRule like below

RewriteRule ^/ShowGuide/?$ https://<website>/help.pdf [L,NC,R,QSA]

With Chrome 61, the PDF was not opening, in console it was showing the message

"Resource interpreted as Document but transferred with MIME type application/pdf: "

We tried to add mime type in the rewrite rule as below but it didn't help.

RewriteRule ^/ShowGuide/?$ https://<website>/help.pdf [L,NC,R,QSA, t:application/pdf]

I have updated my Chrome to latest 62 version and it started to showing the PDF again. But the message is still there in the console.

With all other browsers, it was/is working fine.


m
milkyWay

The problem

I had similar problem. Got message in js

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

But I also got message in chrome console

Mixed Content: The site at 'https://my-site/' was loaded over a secure connection, but the file at 'https://my-site/Download?id=99a50c7b' was redirected through an insecure connection. This file should be served over HTTPS. This download has been blocked

It says here that you need to use an secure connection (but scheme is https in message already, strangely...).

The problem is that href for file downloading builded on server side. And this href used http in my case.

The solution

So I changed scheme to https when build href for file downloading.


h
holdfenytolvaj

In my case the file name was too long, and got the same error. Once shortened below 200 chars worked fine. (limit might be 250?)


B
Brian Deterling

After a couple of csv file downloads (lots of tests) chrome asked whether to allow more downloads from this page. I just dismissed the window. After that chrome did not download the file any more but the console sayed:

"Resource interpreted as Document but transferred with MIME type text/csv"

I could solve that issue by restarting chrome (completely Ctrl-Shift-Q).

[Update] Not sure why this post was deleted but it provided the solution for me. I had gotten the message earlier about trying to download multiple files and must have answered no. I got the "Resource interpreted..." message until I restarted the browser; then it worked perfectly. For some cases, this may be the right answer.


R
Rashi Abramson

I got this error because I was serving from my file system. Once I started with a http server chrome could figure it out.


e
eradima

I was experiencing the same trouble with a download manager I created. The problem I was having involved the file name being too long and the extension being clipped off.

Example: File Name : Organizational Protocols and Other Things That are Important.pd

<?php
  header("Content-Disposition: attachment; filename=$File_Name");
?>

Solution: Increased the MySQL database field to 255 to store the file name, and performed a length check before saving the blob. If the length > 255 trim it down to 250 and add the file extension.


F
Faraz

Try below code and I hope this will work for you.

var Interval = setInterval(function () {
                if (ReportViewer) {
                    ReportViewer.prototype.PrintReport = function () {
                        switch (this.defaultPrintFormat) {
                            case "Default":
                                this.DefaultPrint();
                                break;
                            case "PDF":
                                this.PrintAs("PDF");
                                previewFrame = document.getElementById(this.previewFrameID);
                                previewFrame.onload = function () { previewFrame.contentDocument.execCommand("print", true, null); }
                                break;
                        }
                    };
                    clearInterval(Interval);
                }
            }, 1000);

O
Olivier B.

I've faced this today, and my issue was that my Content-Disposition tag was wrongly set. It looks like for both pdf & application/x-zip-compressed, you're supposed to set it to inline instead of attachment.

So to set your header, Java code would look like this:

...
String fileName = "myFileName.zip";
String contentDisposition = "attachment";
if ("application/pdf".equals(contentType)
    || "application/x-zip-compressed".equals(contentType)) {
    contentDisposition = "inline";
}
response.addHeader("Content-Disposition", contentDisposition + "; filename=\"" + fileName + "\"");
...

R
RWC

The problem

I literally quote Saeed Neamati (https://stackoverflow.com/a/6587434/760777):

In your request header, you have sent Content-Type: text/html which means that you'd like to interpret the response as HTML. Now if even server send you PDF files, your browser tries to understand it as HTML.

The solution

Send the bloody correct header. Send the correct mime type of the file. Period!

How?

Aaah. That totally depends on what you are doing (OS, language).

My problem was with a dynamically created download link in javascript. The link is for downloading an mp3 file. An mp3 file is not a document, neither is a pdf, a zip file, a flac file and the list goes on.

So I created the link like this:

download

and I changed it to this:

download

Problem solved. Adding an extra attribute to the form tag solved it. But there is no generic solution. There are many different scenario's. When you send a file from the server (you created it dynamically with a language like CX#, Java, PHP), you have to send the correct header(s) with it.

Side note: And be careful not to send anything (text!) before you send your header(s).


A
Alexander Ramos

I got the same error, the solution was to put the attribute

target = "_ blank"

Finally :

<a href="/uploads/file.*" target="_blank">Download</a>

Where * is the extension of your file to download.


this is not a solution. It just opens a new tab and download the file in that tab. Any error / warning occurred also would have been reported in that tab's debug console and not in the present tab's debug console. Now, you just don't see the problem. But, actually it is not fixed.