问题
I am wanting to zip and download a folder. I can't seem to find out how. JSZip seems to only download files.
I don't want to have to use PHP.
Last thing, I want it to download with a link, like <a href='#'>Download</a>.
回答1:
Just keep calling zip.file(). Look at the example from their http://stuk.github.io/jszip/
for example like below :
var zip = new JSZip();
// Add a text file with the contents "Hello World\n"
zip.file("Hello.txt", "Hello World\n");
// Add a another text file with the contents "Goodbye, cruel world\n"
zip.file("Goodbye.txt", "Goodbye, cruel world\n");
// Add a folder named "images"
var img = zip.folder("images");
// Add a file named "smile.gif" to that folder, from some Base64 data
img.file("smile.gif", imgData, {base64: true});
var content = zip.generate();
location.href="data:application/zip;base64,"+content;
The important thing is to understand the code you've written - learn what each line does. If you do this, you'd realize that you just need to call zip.file() again to add another file.
来源:https://stackoverflow.com/questions/37624807/cant-download-folder-with-jszip