This is crazy but I don't know how to do this, and because of how common the words are, it's hard to find what I need on search engines. I'm thinking this should be an easy one to answer.
I want a simple file download, that would do the same as this:
<a href="file.doc">Download!</a>
But I want to use an HTML button, e.g. either of these:
<input type="button" value="Download!">
<button>Download!</button>
Likewise, is it possible to trigger a simple download via JavaScript?
$("#fileRequest").click(function(){ /* code to download? */ });
I'm definitely not looking for a way to create an anchor that looks like a button, use any back-end scripts, or mess with server headers or mime types.
You can hide the download link and make the button click it.
I think this is the solution you were looking for
I hade a case where my Javascript generated a CSV file. Since there is no remote URL to download it I use the following implementation.
For the button you can do
Another way of doing in case you have a complex URL such as
file.doc?foo=bar&jon=doe
is to add hidden field inside the forminspired on @Cfreak answer which is not complete
I have done some research and found the best answer. You can trigger a download by using the new HTML5
download
attribute.Where :
path_to_file
is either an absolute or relative path,proposed_file_name
the filename to save to (can be blank, then defaults to the actual filename).Hope this is helpful.
Whatwg Documentation
Caniuse
If your looking for a vanilla JavaScript (no jQuery) solution and without using the HTML5 attribute you could try this.