我有一个用HTML / Javascript编写的UWP应用程序,我无法保存由JSZip制作的zip文件。特别是把它写到磁盘是我挂断的地方。
我在Microsoft文档中看到有WriteBufferAsync,WriteBytesAsync,WriteLinesAsync和WriteTextAsync。我不确定我需要哪一个。此外,JSZip可以生成不同的类型,如base64,binarystring,uint8array,arraybuffer和blob。我只是不确定我需要将此zip文件写入用户磁盘的组合。
以下是我的代码:
savePNGButton.addEventListener('click', function (e) {
var zip = new JSZip();
if (WatermarkText === ""){
ZipFolder = zip.folder("ImageFolder");
} else {
ZipFolder = zip.folder(WatermarkText);
}
$(".WatermarkPhoto").each(function(index) {
imgsrc = this.src;
var DataURL = imgsrc.replace('data:image/png;base64,', '');
ZipFolder.file(WatermarkText + index + ".png", DataURL, { base64: true });
});
zip.generateAsync({ type:"blob"})
.then(function (content) {
console.log(content);
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
savePicker.fileTypeChoices.insert("ZIP archive", [".zip"]);
savePicker.suggestedFileName = WatermarkText+".zip";
savePicker.pickSaveFileAsync().then(function (file) {
if (file) {
Windows.Storage.CachedFileManager.deferUpdates(file);
Windows.Storage.FileIO.writeTextAsync(file, content).done(function () {
Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
console.log("File " + file.name + " was saved.");
} else {
console.log("File " + file.name + " couldn't be saved.");
}
});
});
} else {
console.log("Operation cancelled.");
}
});
});
});
答案 0 :(得分:0)
如果有人碰到这个,我发现这个链接让我按正确的方向发送https://blog.appliedis.com/2013/09/18/zipping-and-unzipping-files-in-a-winjs-application/我使用了来自JSZip的uint8array以及来自windows FileIO类的文件流和WriteBytesAsync。下面是我用来压缩和显示文件保存对话框的最后一段代码。
savePNGButton.addEventListener('click', function (e) {
var zip = new JSZip();
if (WatermarkText === ""){
ZipFolder = zip.folder("Images");
} else {
ZipFolder = zip.folder(WatermarkText);
}
$(".WatermarkPhoto").each(function(index) {
imgsrc = this.src;
var DataURL = imgsrc.replace('data:image/png;base64,', '');
ZipFolder.file(WatermarkText + index + ".png", DataURL, { base64: true });
});
zip.generateAsync({ type: "uint8array", streamFiles:"true"})
.then(function (content) {
console.log(content);
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
savePicker.fileTypeChoices.insert("ZIP archive", [".zip"]);
savePicker.suggestedFileName = WatermarkText + ".zip";
savePicker.pickSaveFileAsync().then(function (file) {
if (file) {
Windows.Storage.CachedFileManager.deferUpdates(file);
Windows.Storage.FileIO.writeBytesAsync(file, content).done(function () {
Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
console.log("File " + file.name + " was saved.");
} else {
console.log("File " + file.name + " couldn't be saved.");
}
});
});
} else {
console.log("Operation cancelled.");
}
});
});
});