简体   繁体   English

JSZip创建的名称.zip文件

[英]Name .zip file created by JSZip

I am using JSZip and I am creating a .zip file with several .xml files inside it as shown below: 我正在使用JSZip,并且正在创建一个.zip文件,其中包含多个.xml文件,如下所示:

// get each xml in string format and add it to zip object
let zip = new JSZip();
for(let i = 0; i < containers.length; i++){
    let xml = getXML(i);
    zip.file("file"+i+".xml", xml);
}

// download the .zip file
zip.generateAsync({
    type: "base64"
}).then(function(content) {
    window.location.href = "data:application/zip;base64," + content;
});

The .zip file is created and downloaded perfectly but the name of the file is the default "download file". .zip文件可以完美创建和下载,但是文件名是默认的“下载文件”。 What i want to do is give a name to this file at will (for example allXMLs.zip ). 我想做的就是随意 给该文件命名 (例如allXMLs.zip )。

I looked at the JSZip documentation but I did not find anything really enlightening, any help would be greatly appreciated. 我查看了JSZip文档,但没有发现任何真正有启发性的内容,我们将不胜感激。

You could create an anchor tag with a 'download' attribute that would allow you some control over the filename. 您可以使用'download'属性创建锚标记,从而可以对文件名进行一些控制。

zip.generateAsync({
    type: "base64"
}).then(function(content) {
    var link = document.createElement('a');
    link.href = "data:application/zip;base64," + content;
    link.download = "your-file-name.zip";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
});

In the documentation, it suggests an additional library called [FileSaver.js]. 在文档中,它建议使用另一个名为[FileSaver.js]的库。 1 See here for the example given in the projects How To. 1请参见此处的“如何做”项目中给出的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM