使用 JSZIP 设置自定义文件名

set custom filename with JSZIP

我已经编写了一个 zip 下载,但我面临的问题是我无法为 .zip 指定一个自定义文件名它一直给我一些像这样的随机名称 "ff22f3dc-24dc-41cb-b83d-06acef1694d0.zip".是否有选项或类似的设置?

我初始化下载的方式是使用这段代码。顺便说一句,我没有使用 'a' 标签。

jszip.generateAsync({type:"blob"})
                .then(function(content)
                {
                    window.location = URL.createObjectURL(content);
                });
        } 

我很感谢你的每一个建议。

编辑:此 zip 包含 Excel 个文件(使用 JSZIP Utils)

根据 this page in the documentation, it's possible to use the FileSaver polyfill 为您的下载指定一个自定义文件名。虽然它只适用于现代浏览器,但您需要使用功能检测来回退到默认行为。

看看这是否有效:

try {
    var isFileSaverSupported = !!new Blob;
} catch (e) {}

jszip.generateAsync({ type: "blob" })
    .then(function(content) {
        if(isFileSaverSupported) {
            saveAs(content, "custom_filename.zip");
        } else {
            window.location = URL.createObjectURL(content);
        }
    });