To download and name the .zip folder you will need saveAs() from file-saver
import { saveAs } from "file-saver";
zip
.generateAsync({
type: "blob"
})
.then(content => {
saveAs(content, "fileName.zip");
});
Answer from MedEqz on Stack OverflowTo download and name the .zip folder you will need saveAs() from file-saver
import { saveAs } from "file-saver";
zip
.generateAsync({
type: "blob"
})
.then(content => {
saveAs(content, "fileName.zip");
});
Whats happening is that .generateAsync is returning a promise. You will have to wait until the promise is either fulfilled or rejected before being able to work with the data.
Promises is a core concept in JS when working with async operations. You can read more about promises here
import zipTargetFiles from '/path'
zipTargetFiles( data ).then(file => {
// Access your file here
})
» npm install jszip
Anyone coming across this can use JSZip-utils and write the following code
JSZipUtils.getBinaryContent("../path/file.zip", function (err, data) {
if (err) {
throw err;
}
const jsZip = new JSZip();
jsZip.loadAsync(data).then(function (zip) {
Object.keys(zip.files).forEach(function (filename) {
zip.files[filename].async("base64").then(function (fileData) {
const image = document.createElement("img");
image.src = "data:image/*;base64," + fileData;
const unziped = document.querySelector(".unziped-container");
unziped.appendChild(image);
});
});
});
});
the documentation seems pretty clear: the first argument to loadAsync is the zip file data, but you're passing it a string.
(and what you're tring to do won't work anyway. your React code is running in the browser and has no knowledge of filesystem paths or filenames.)