Search code examples
javascriptelectronjszipfilesaver.js

JSZip temporary file location


I have a electron application using jszip to create a zip file that the user is then able save. Everything works fine but my problem is my application uses the users download folder. Im guessing its making a temporary file. I've submitted my application to the mac store and they want me to use another location instead of the users downloads folder for the temporary file. Is there anyway I can specify the temporary location or maybe something else other then jszip that will do this?

Here is the code I use

savePNGButton.addEventListener('click', function(e) {
  var zip = new JSZip();
  if (WatermarkText == ""){
    var img = zip.folder("images");
  } else {
    var img = zip.folder(WatermarkText);
  }
  $(".WatermarkPhoto").each(function(index) {
   imgsrc = this.src;
   var DataURL = imgsrc.replace('data:image/png;base64,','');
   img.file(WatermarkText+index+".png", DataURL, {base64: true});
  });
  zip.generateAsync({type:"blob"})
  .then(function(content) {
      saveAs(content, WatermarkText+".zip");
  });
});

[edit] Looking more into this it looks like my problem is not with JSZip but with chrome or FileSaver.js using the downloads folder as a temp folder for the file before the users chooses where to place the file. Is there anyway I can change the temp location for my electron app?


Solution

  • If anyone comes across this, I never figured a way around the HTML5 filesystem way of moving the temp file before the users selects the download location. Instead I am using nodejs file system with electrons showSaveDialog. I also had to change JSZip to use .generateNodeStream instead of .generateAsync. Below is my function that I got working for me.

    savePNGButton.addEventListener('click', function(e) {
      var zip = new JSZip();
      if (WatermarkText == ""){
        var img = zip.folder("images");
      } else {
        var img = zip.folder(WatermarkText);
      }
      $(".WatermarkPhoto").each(function(index) {
       imgsrc = this.src;
       var DataURL = imgsrc.replace('data:image/png;base64,','');
       img.file(WatermarkText+index+".png", DataURL, {base64: true});
      });
      // zip.file("file", content);
      // ... and other manipulations
        dialog.showSaveDialog({title: 'Test',defaultPath: '~/'+WatermarkText+'.zip',extensions: ['zip']},(fileName) => {
          if (fileName === undefined){
              console.log("You didn't save the file");
              return;
          }
        zip
        .generateNodeStream({type:'nodebuffer',streamFiles:true})
        .pipe(fs.createWriteStream(fileName))
        .on('finish', function () {
            // JSZip generates a readable stream with a "end" event,
            // but is piped here in a writable stream which emits a "finish" event.
            console.log("zip written.");
        });
        });
    });