0

I am trying to download png image with javascript.

Problem:

First it downloads image actually opens up in a new tab for user download,but after that whenever i click to download it doesnt downloading anything. But it works fine on other browser, issue with safari only.

Below what i have tried so far:

        imgData  = canvas.toDataURL('image/png');
        link = document.createElement('a');
        link.setAttribute('download', projectName+'.png');
        link.setAttribute('target', "_blank");
        link.setAttribute('href', imgData);
        link.click();
        link.remove();
sandeepKumar
  • 771
  • 2
  • 12
  • 33

1 Answers1

0

There is no download attribute for a tag in safari

You can use FileSaver.js library. See how to use?

another method: You can do form submit this works only if content-disposition and content-type header present in response headers

For example:

$('body').append('<from id="fileDownload" action="< URL to download >" method="< HTTP method >" </form>');

$('#fileDownload').submit(); $('#fileDownload').remove();

Community
  • 1
  • 1
Vinoth Rajendran
  • 1,181
  • 1
  • 13
  • 28