-3

I used below code for the create and download the file. But below the code only work when click the link. so how to download the file by default load. without click.

     <script>

array=[{a:'1',b:'2'},{x:'3',y:'4'}];

function dl(array,filename){
var b=document.createElement('a');
b.download=filename;
b.textContent=filename;
b.href='data:application/json;base64,'+window.btoa(unescape(encodeURIComponent(JSON.stringify(array))))
return b
}

document.body.appendChild(dl(array,'my.json'));


        </script>
Maninblack
  • 743
  • 1
  • 8
  • 17

2 Answers2

1

If I understand your question properly the solution would be to replace b.href with window.location your content will automatically load in the current tab.

1

Please try the below code.

var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");
var img = zip.folder("images");
img.file("smile.gif", imgData, {base64: true});
var content = zip.generate({type:"blob"});
// see FileSaver.js
saveAs(content, "example.zip");
Lemon
  • 82
  • 1
  • 13