Dane411
Dane411

Reputation: 863

Download file without extension with JavaScript

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

Answers (1)

baao
baao

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

Related Questions