0

My original file wouldn't download using the local method so I decided to use Node.js, as it's already packed in AppJS, and still the zip file won't execute in AppJS.

$(".export").on("click", function() {
  var fs = require("fs");
  var JSZip = require("jszip");

  var zip = new JSZip();
  zip.file("hello.txt", "Hello node!");

  var content = zip.generate({type:"nodebuffer"});
  // saveAs(content, "test.zip");

  fs.writeFile("test.zip", content, function(err) {
    if (err) throw err;
  });
});
body {
  background: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://stuk.github.io/jszip/dist/jszip.min.js"></script>
<script src="http://stuk.github.io/jszip-utils/dist/jszip-utils.js"></script>
<script src="http://stuk.github.io/jszip/vendor/FileSaver.js"></script>

<button class="export">Download</button>

Note: I've tried saving files using the File API, but the only want I've been able to successfully write a file in AppJS is by using Node.js as seen below.

var fs = require("fs");
fs.writeFile("hello.txt", "Hi", function(err) {
  if (err) throw err;
});
halfer
  • 19,824
  • 17
  • 99
  • 186
Michael Schwartz
  • 8,153
  • 14
  • 81
  • 144

1 Answers1

0

I wasn't using Node but instead doing something similar in the browser that required FileSaver.js as well.

Have you tried:

var JSZip = require('jszip');
var saveAs = require('filesaver.js');

var zip = new JSZip();
zip.file("hello.txt", "Hello node!");

var blob = zip.generate({type: 'blob'});
saveAs(blob, 'images.zip');

The main difference here using {type: 'blob'} instead of nodebuffer. I was successful setting the response type to arraybuffer using a simple XHR download module.

module.exports = function download(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.responseType = 'arraybuffer';

  xhr.onload = function(e) {
    callback(null, xhr.response);
  };

  xhr.onerror = function(e) {
    callback(status.response);
  };

  xhr.send(null);
};
Filuren
  • 661
  • 2
  • 7
  • 19