Active Oldest Votes. Improve this answer. Ayush Gupta Ayush Gupta 5, 2 2 gold badges 20 20 silver badges 17 17 bronze badges. At the time of this comment, the download attribute is limited to Chrome, Firefox and Opera. Even recent versions of IE and Safari do not support it. For future support: check caniuse.
Netbeans error checking complains about it, but it seems to work fine. This works in Edge at the time of writing this comment, and seems like the only way possible to stop hyperlinked PDFs opening in Edge's very poor attempt at a PDF viewer. All latest. Is working on all except on Mozilla. This only works for same-origin links, as mentioned on caniuse.
If you don't have access to the server, then you can try to proxy it and set the response header manually. Show 2 more comments. DrWaky DrWaky 6 6 silver badges 6 6 bronze badges. It's exactly the same as the current top answer though, from Ayush Gupta in If you give a string in the download attribute, it will be used as file name. I'm using it in userscripts all the time. Correct me if I'm wrong, but download is not a "save-as" dialog box. Works only with same origin! Karel Petranek Karel Petranek In practice I believe this is widely implemented.
This is the best solution, hassle free and works. You can directly download any file type using this method. Content-Encoding: none is useless. Show 1 more comment. Tony Tony 3 3 silver badges 9 9 bronze badges. This worked well, since I had a particular document directory for downloads. I omitted the ForceType, just to let the types stay as-is.
I also didn't need the case-insensitive; mine seems to be case-insensitive already. This worked wonderfully, even in OSX Safari, where as other answers including the accepted one, as one commenter pointed out there are limited to certain browsers which support the given HTML feature.
This one seems more ideal to me since it worked across the board, though it does require higher-level access to the server configuration files. You could put that right in your apache. How would a Windows-hosted web site implement this, since they can't read htaccess files?
User User 1 1 silver badge 4 4 bronze badges. Turns out the file needs to be local in order for it to work. My href was a url to cloud storage. Ashay Ashay 4 4 silver badges 13 13 bronze badges. This appears to be a nice way to serve up every single file on your server to anyone who's paying attention.
Try adding this line to your. Sharad Gautam Sharad Gautam 89 1 1 silver badge 1 1 bronze badge. I'd be interested to know why this has been downvoted - it's one of the simplest methods and works: css-tricks. People try to modify the core functionality of a server on free hosting sites and then downvote because they can't get it to work. Every other solution will have caveats because it is a "hack" to replace this. I'm downvoting it because it's the wrong solution to the problem.
It's a bad idea to mess with types of resources. NathanHornby - I didn't downvote it, but this would not work for any Windows hosted site, since they don't use. I didn't downvote, but this solution blindly affects ALL files of that suffix. It's indiscriminate and therefore won't be appropriate in all cases.
I just used this, but I don't know if it works across all browsers. 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. You can alternatively explain to the user that they should right click on the link and select download. If you have no other alternatives to force the file to download, you can host it on a file hosting service such as Google Docs.
The most common file types that are affected by this behavior are PDF files and images. The code below will tell the browser to prompt the user to save the file. This link does not have the download attribute. This link has the download attribute. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in JavaScript. Most visited in Web Technologies.
Installation of Node. Web 1. We use cookies to ensure you have the best browsing experience on our website. Start Your Coding Journey Now!
0コメント