我的原始文件不会使用本地方法下载,所以我决定使用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>
注意:我尝试使用 File API 保存文件,但我能够在 AppJS 中成功写入文件的唯一需要是使用 Node.js,如下所示。
var fs = require("fs");
fs.writeFile("hello.txt", "Hi", function(err) {
if (err) throw err;
});