How to stop automatic download on IE 11 while usin

2019-07-17 06:28发布

问题:

I have given a source in a Iframe tag, my is issue is that when the page loads on IE the download begins automatically and it generally happens on IE installed on windows 8.

<div> <iframe  src="../../Images/Sample.pdf" width="800px" height="800px" ></iframe> </div>

回答1:

It's downloaded probably because there is not Adobe Reader plug-in installed. In this case IE (it doesn't matter which version) doesn't know how to render it and it'll simply download file (Chrome, for example, has its own embedded PDF renderer).

That said. is not best way to display a PDF (do not forget compatibility with mobile browsers, for example Safari). Some browsers will always open that file inside an external application (or in another browser window). Best and most compatible way I found is a little bit tricky but works on all browsers I tried (even pretty outdated):

Keep your but do not display a PDF inside it, it'll be filled with an HTML page that consists of an tag. Create an HTML wrapping page for your PDF, it should look like this:

<html>
<body>
    <object data="your_url_to_pdf" type="application/pdf">
        <embed src="your_url_to_pdf" type="application/pdf" />
    </object>
</body>
</html>

Of course you still need the appropriate plug-in installed in the browser. Also take a look to this post if you need to support Safari on mobile devices.

1st. Why nesting inside ? You'll find answer here on SO. Instead of nested tag you may even provide a custom message for your users (or a built-in viewer, see next paragraph).

2nd. Why an HTML page? So you can provide a fallback if PDF viewer isn't supported. Internal viewer, plain HTML error messages/options and so on...

It's tricky to check PDF support so you may provide an alternate viewer for your customers, take a look to PDF.JS project, it's pretty good but rendering quality - for desktop browsers - isn't as good as a native PDF renderer (I didn't see any difference in mobile browsers because of screen size, I suppose).

See also: HTML embedded PDF iframe