Force pdf download instead of open html5
As of this post, all major desktop browsers excluding Microsoft IE support the download attribute. It is likely that people who use these browsers would likely rather view the document rather than saving it to their phone. New to HTML 5. This markup is easier to understand and is supported by all modern browsers, but may not be supported by all content management systems. If for whatever reason, you cannot add the download attribute in the case that you cannot directly edit the HTML of your web page , you can optionally compress the file using zip, and instruct the user to download the zip file.
The basic idea of his solution is to use the Apache servers header mod and edit the. The second reason was because time is a consideration. If you know how to turn on an Apache mod and edit the. It requires Linux hosting not Windows. This may not be appropriate approach for all uses as it requires high level server access to configure. If you are using HTML5 and i guess now a days everyone uses that , there is an attribute called download. As the html5 way my previous answer is not available in all browsers, heres another slightly hack way.
Now set location. This will cause the browser to download the file. Fiddling with the media-type could yield something. Reference from: This blog. A really simple way to achieve this, without using external download sites or modifying headers etc. BTW great question, I was looking for an answer as well, since most browser-embedded PDF plugins take sooo long to display anything and will often hang the browser whilst the PDF is loading.
A server-side solution is more compatible, until the "download" attribute is implemented in all the browsers. The links that point to the filestore are generated like this:. You also can add it to parent div and will pickup all links inside it. A very easy way to do this, if you need to force download for a single link on your page, is to use the HTML5 download-attribute in the href-link.
There has been a debate whether this is good practice or not, but in my case I have an embedded viewer for a PDF file and the viewer does not offer a download link, so i have to provide one separately. Here I want to make sure the user does not get the PDF opened in the web browser, which would be confusing. This won't necessary open the save as-dialog, but will download the link straight to the preset download destination.
And of course if you are doing a site for someone else, and need them to write in manually attributes to their links is probably a bad idea, but if there is way to get the attribute into the links, this can be a light solution. Add a response header Content-Disposition:attachment; followed by the file name.
Remove the Meta Content-Disposition;Inline; which will open the document in the same window. If you have a plugin within the browser which knows how to open a PDF file it will open directly. Like in case of images and HTML content. So the alternative approach is not to send your MIME type in the response.
In this way the browser will never know which plugin should open it. I just had a very similar issue with the added problem that I needed to create download links to files inside a ZIP file. I first tried to create a temporary file, then provided a link to the temporary file, but I found that some browsers would just display the contents a CSV Excel file rather than offering to download.
Eventually I found the solution by using a servlet. The servlet takes as input a full path name to the ZIP file, and the name of the file inside the zip that should be downloaded. The following syntax will make it happen. With large PDF files the browser hangs.
In the Action drop down, select Always ask. Now I am able to download e-books! Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Force to open "Save As Asked 11 years, 1 month ago. Active 3 months ago. Viewed k times. Peter Mortensen I tried your updated solution, artmania - but the same problem I've been having in Safari occurred. It requires Linux hosting not Windows.
This may not be appropriate approach for all uses as it requires high level server access to configure. As such, if you have said access it's probably because you already know how to do those two things. If not, check Nick's blog for more instructions. As the html5 way my previous answer is not available in all browsers, heres another slightly hack way.
Now set location. This will cause the browser to download the file. Unfortunately you can't set file name or extension this way. Fiddling with the media-type could yield something. I needed to do this for files created with dynamic names in a particular folder and served by IIS.
If you are using HTML5 and i guess now a days everyone uses that , there is an attribute called download. I've had some issues with the suggested solution that creates an a DOM element, and sets the download attribute. It still displayed a popup warning in some browsers perhaps they got a little stricter by Adding the pdf mime type to the href attribute solved the browser popup warning, but it messed up the file the downloaded file got damaged and couldn't be opened.
His code example uses an npm library though. Here's how to do it using js only:. The behaviour should depend on how the browser is set up to handle various MIME types. I recommend against this as it should be the users choice what will happen when they open a PDF file. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 4 months ago.
Active 1 month ago. Viewed k times. MikeD 4, 1 1 gold badge 23 23 silver badges 39 39 bronze badges. This is to my knowledge not scriptable behavior.
0コメント