I have a UWP app written in HTML/Javascript and im having trouble saving a zip file made from JSZip. Specifically writing it to disk is where I am hung up at.
I see in the Microsoft docs that there is WriteBufferAsync, WriteBytesAsync, WriteLinesAsync, and WriteTextAsync. Im not sure which one I need for this. Also JSZip can generate different types like base64, binarystring, uint8array, arraybuffer, and blob. Im just not sure what combination I need to write this zip file to the users disk.
Below is my code:
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.");
}
});
});
});
If anyone comes across this, I found this link that sent me in the correct direction https://blog.appliedis.com/2013/09/18/zipping-and-unzipping-files-in-a-winjs-application/ I used uint8array from JSZip along with file streaming and WriteBytesAsync from the windows FileIO class. Below is the final block of code i used to zip and present a file save dialog.
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.");
}
});
});
});