I am using below code in Javascript controller of Lighting Component to download attachments on Case. It's working for pdf and zip files, but images are being opened in a new tab. Is there any way to force browser to download images rather than opening them through Javascript?

var a = document.createElement('a');
a.href = 'some url';
  • Sounds like a potential security vulnerability in the browser which would be undesirable.
    – Adrian Larson
    Commented Nov 18, 2019 at 18:58
  • What can be done about it? We have tried from two different computers, same result. When I run it from sandbox, it works fine. Commented Nov 18, 2019 at 19:20

2 Answers 2


Add the download attribute to your anchor element:

a.download = 'desired-filename.ext';

Where desired-filename.ext is the name of the file, such as cute-fox.png or kitchen.jpg.

  • it still didn't work Commented Nov 19, 2019 at 17:31

You can use anchor tag with download attribute:

<a href="urlToTheFile" download>Download Me!</a>

But I believe you should stick to the same origin policy. If you are trying to download the source from another site it won't download, only open it in the same window. You can add target="_blank" attribute to open it in a new window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .