Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a Download File prompt instead of displaying it in-browser with HTML? [duplicate]

Tags:

html

 <a href="sample.pdf" target="_blank">Download</a> 

If I click Download button, this target blank is opening a new window.

But I need it to prompt a dialog for saving this file. How can I achieve this? enter image description here

like image 511
UI_Dev Avatar asked May 23 '14 09:05

UI_Dev


People also ask

How do I force a file to download instead of open in browser?

Click on "Settings" and you'll see a new page pop up in your Chrome browser window. Scroll down to Advanced Settings, click Downloads, and clear your Auto Open options. Next time you download an item, it will be saved instead of opened automatically.

How do I trigger a download in HTML?

To trigger a file download on a button click we will use a custom function or HTML 5 download attribute. The download attribute simply uses an anchor tag to prepare the location of the file that needs to be downloaded.

How do I get PDF to open instead of browser HTML?

Click “Site Settings” on the right. Scroll down in Site Settings and click “Additional content settings” at the very bottom. In the expanded menu, select “PDF documents.” Toggle on the “Download PDF files instead of automatically opening them in Chrome” option.

How do I make Chrome download instead of opening?

Alternatively, enter chrome://settings/content/ in the address bar. On the right, go to the Content section, and click on Additional content settings. Click on PDF documents. On the next page, turn on (enable) the Download PDF files instead of automatically opening them in Chrome option.

How to force a browser to download a file instead of navigating?

A special download attribute can be used inside of an <a href> tag that will tell the browser to download the file instead of navigating to it. The code below will tell the browser to prompt the user to save the file.

How can I control when the file download prompt appears?

As long as you have the ability to edit the HTML code of the page, you can use a simple HTML tweak that will allow you to control when the file download prompt appears if the user clicks a link or a button. A special download attribute can be used inside of an <a href> tag that will tell the browser to download the file instead of navigating to it.

How to tell if a file is supported by the browser?

This topic has 12 replies, 7 voices, and was last updated 7 years, 1 month ago by HowardYeomans. Usually when a user goes to a file URL (for example: a download link), the file will show in the browser if the browser supports it. Image files like jpg, png, gif etc. will almost always show in the browser.

How do I let people download a file from my website?

For example, let’s say you have a PDF receipt or an MP3 file that you want to let people download. You might point that to that file with a link. In most browsers, clicking on the link will open the file directly in the browser. But, if you add the download attribute to the link, it will tell the browser to download the file instead.


1 Answers

This is something that you cannot absolutely control with HTML itself.

If the user is having a browser with PDF reading capabilities (or a plugin) and the corresponding settings to open PDF files in-browser, the PDF will open like that.

The PDF opens in a new tab simple because of your target="_blank", which has nothing to do with a download prompt.

If you are using HTML5 you can use the download attribute:

<a href="sample.pdf" download="sample.pdf">Download</a> 

If you have a back-end service which you can control or you feel like fiddling with your Web Server, you can always look for setting the right Content-Disposition. See this SO question for some nice discussion on Content-Disposition.

like image 198
Kostas Rousis Avatar answered Sep 20 '22 09:09

Kostas Rousis