Reputation: 863
I wanna download some files through js.
The following code works fine when the file has an extension ie. http://example.com/img.jpg, but when it doesn't ie. http://example.com/img it just redirects me to a blank page with the file, as a normal link does.
function downloadURI(uri) {
var link = document.createElement('a');
link.href = uri;
link.click();
}
How do I get over this issue, and make the browser to download?
Upvotes: 0
Views: 5317
Reputation: 73271
Simply tell your browser that it's a download:
function downloadURI(uri) {
var link = document.createElement('a');
link.href = uri;
link.download = 'download';
link.click();
}
downloadURI('test')
Upvotes: 4