Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome error on downloading file via iframe

I'm submitting JSON data to a server. The server generates a PDF file and responds with the URL to that PDF file. I then make another request to the server to download the PDF file. To do the download, I create a hidden iframe and set the source the the PDF url. Note that I want the user's browser to stay on the same page and download in the background. More details about what I'm doing and the solution I went with can be found in this SO question.

Whenever I use this technique in Chrome, everything works fine. But looking at the chrome developer console, I see the error message Failed to load resource. Is there a way to do this differently so that I won't get these errors?

A very simple and working example of the problem in action can be seen on jsfiddle. Just click the Download PDF button and look at your Chrome console.

The code on that page looks like this:

$(document).ready( function() {
    var downloadFile = function(url){
        var iframe = $("iframe#download");
        if (!iframe.length) {
            iframe = $("<iframe id='download' style='display:none;'/>");
            $("body").append(iframe);
        }
        iframe.attr("src", url);
    };

    $("button").click(function() {    
        downloadFile("http://www.education.gov.yk.ca/pdf/pdf-test.pdf");
    });
});
like image 920
Tauren Avatar asked Dec 04 '10 00:12

Tauren


People also ask

Why is iFrame not working in Chrome?

Why iFrame is not working in Chrome? It's really a simple issue here because Chrome is usually blocking iFrame and that's the main reason you're getting the error. However, iFrame may also be blocked from your Internet Options, by your antivirus or by an add-on you just installed in Chrome.

Why are my PDFs not downloading?

Typically, this occurs for one of the following reasons: Your computer is not connected to the Internet, or there is a problem with your Internet settings. Your antivirus software needs to be updated. You may not be connected to the Adobe server.

How do I disable the PDF toolbar button in iFrame jQuery?

load(function(){ jQuery('#iframe'). contents(). find("#toolbarViewerRight"). hide(); });


3 Answers

Other respondents have suggested changing the content-type header, which should work just as well, but there's a specific response header for instructing the browser how to treat a file.

The following should force the pdf to be downloaded rather than displayed in the browser:

"Content-disposition: attachment; filename=[your file's name]"

like image 55
R Hill Avatar answered Oct 26 '22 22:10

R Hill


This is just off the top of my head -- I believe I've run into the same issue before. When the iframe is first added to the dom, it has no src -- which makes Chrome "FAIL TO LOAD RESOURCE".

Try adding an initial src to the iframe --

 iframe = $("<iframe id='download' src='about:blank' style='display:none;'/>");

That should do it I believe. (you can downvote me a zillion times if it's wrong :))

like image 33
ansiart Avatar answered Oct 26 '22 22:10

ansiart


Chrome has a built-in PDF viewer. When you set the src of the iframe, Chrome just tries to display the PDF file. Since it's set to display:none, the download gets canceled and you get that message.

Try setting display to something other than none and position the iframe off the screen with position:absolute.

like image 31
mattbasta Avatar answered Oct 26 '22 23:10

mattbasta