1

I am using chrome.

This is my code.

var save = document.createElement('a');
save.href = fileURL;
save.download = fileName;
alert(save.download);
save.click();

It works well but it doesn't change the image name. It saves as "download.png".

What is wrong?

DutGRIFF
  • 5,103
  • 1
  • 33
  • 42
Somputer
  • 1,223
  • 2
  • 11
  • 20
  • 1
    If your problem is what I think it is, this is the answer for it: http://stackoverflow.com/a/6943481/240443 – Amadan Jun 20 '14 at 02:01

1 Answers1

3

It would help to see what fileURL and fileName are set to but I am guessing that is your problem. In Firefox and Chrome you have to use the relative path to the image. It will not work on remote images.

MDN says:

In Firefox 20 this attribute is only honored for links to resources with the same-origin.

I have tested this in Chrome and Firefox and it only works if you use the relative path to the image:

 save.href = "images/wonky-download-121938718712348891912.jpg";
 save.download = "coolName.jpg";

I say ralative path because using mysite.com/image.jpg didn't work while image.jpg did.

Update

whatwg.org says:

In cross-origin situations, the download attribute has to be combined with the Content-Disposition HTTP header, specifically with the attachment disposition type, to avoid the user being warned of possibly nefarious activity.

DutGRIFF
  • 5,103
  • 1
  • 33
  • 42
  • This is very rare right answer, regarding this topic which explains why suggested file name does not work (due to different origin). For me currently works only in Edge. – Milos Aug 26 '17 at 10:49