简体   繁体   English

将NodeJS与AppJS一起使用JSZip打包Zip

[英]Using NodeJS with AppJS to Package Zip with JSZip

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. 我的原始文件无法使用本地方法下载,所以我决定使用Node.js ,因为它已经打包在AppJS中 ,而zip文件仍然无法在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. 注意:我曾尝试使用File API保存文件,但我唯一能够在AppJS中成功写入文件的方法是使用Node.js,如下所示。

var fs = require("fs");
fs.writeFile("hello.txt", "Hi", function(err) {
  if (err) throw err;
});

I wasn't using Node but instead doing something similar in the browser that required FileSaver.js as well. 我没有使用Node,而是在需要FileSaver.js的浏览器中执行了类似的操作。

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 . 这里的主要区别是使用{type: 'blob'}而不是nodebuffer I was successful setting the response type to arraybuffer using a simple XHR download module. 我使用一个简单的XHR下载模块成功将响应类型设置为arraybuffer

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);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM