0

According to this answer, you can force the browser to download a resource instead of opening the resource.

However, it's not working for this link, which links to an Unsplash image:

<a id="dataFileLink" href="https://images.unsplash.com/photo-1556911164-1297abe8527c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" download="24.txt" style="">Download File</a>

JSFiddle: https://jsfiddle.net/6cp95ude/

Clicking the link doesn't force a download of the image; it merely opens the image for viewing.

Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • `download` attribute only works on same-origin urls: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a – mulsun Sep 21 '19 at 23:54
  • 1
    @mulsun please post as an answer so you can get credit. this explains why it didn't work. thanks! – Crashalot Sep 21 '19 at 23:58

2 Answers2

2

download attribute only works on same-origin urls: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a

mulsun
  • 563
  • 5
  • 15
-2

Please try this.

`<a id="dataFileLink" href="https://images.unsplash.com/photo-1556911164-1297abe8527c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" download style="">
  <img src="https://images.unsplash.com/photo-1556911164-1297abe8527c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="Download Image">
</a>`
  • Read [How do I format my code blocks?](https://meta.stackoverflow.com/q/251361/4642212). Your HTML is [invalid](https://html5.validator.nu/). This answer doesn’t solve the problem. – Sebastian Simon Sep 21 '19 at 23:47