简体   繁体   中英

window.location.href - Resource interpreted as Document but transferred with MIME type

I have a url that my visitors can visit that triggers an instant file download:

http://localhost:3000/download

Here is the Node.js Express code for this route:

router.get('/download', function(req, res, next){
    res.setHeader('Content-disposition', 'attachment; filename=downloadfile.zip');
    res.setHeader('Content-type', 'application/zip');
    var filestream = fs.createReadStream('downloadfile.zip');
    filestream.pipe(res);
});

So I'm setting my headers properly and serving up the file for download. In Chrome, if I use JavaScript to trigger this download:

window.location.href="/download";

Then Chrome's console warns:

Resource interpreted as Document but transferred with MIME type application/zip: "http://localhost:3000/download".

The file is downloaded correctly however. Just that warning shows up. There are dozens of topics on this warning and everything appears to point to making sure that your headers are set correctly. I'm pretty sure mine are correct.

Here are the response headers:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-disposition: attachment; filename=downloadfile.zip
Content-type: application/zip
Date: Wed, 23 Dec 2015 19:14:47 GMT
Connection: keep-alive
Transfer-Encoding: chunked

And here are the request headers:

GET /download HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.20 Mobile Safari/537.36
Referer: http://localhost:3000/filerequest
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,es;q=0.6
Cookie: dwanonymous_259c8c42aa11d4ff581f55a1b8d75159=ablFleC64VDwlUxLnzJvjesSKP; s_fid=3E452B78AEFE8958-0900CA102BA80FF0; com.somthing.iMAWwwee=36335a074-9eds-cbbc-91a4-4b8d238ddw6d6

I don't see this error in Firefox. In Chrome I see this error whether or not I have "Ask where to save each file before downloading" checked or not in the Settings.

Is this just a warning I should expect visitors to ignore or is there something I can add to the response headers to prevent this?

Have you tried adding the Content-Length header?

router.get('/download', function(req, res, next){

    var stat = fs.statSync('downloadfile.zip');
    res.writeHead(200, {
       'Content-disposition' : 'attachment; filename=downloadfile.zip'
       'Content-Type' : 'application/zip',
       'Content-Length' : stat.size
    });

    var filestream = fs.createReadStream('downloadfile.zip');
    filestream.pipe(res);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM