Search code examples
javascriptjszip

set custom filename with JSZIP


I've coded out a zip-download, but the problem that i'm facing is that i can't manage to give the .zip a custom filename It keeps giving me some random name like these "ff22f3dc-24dc-41cb-b83d-06acef1694d0.zip". Is there an option or something similar to set it?

The way I init my download is by using this piece of code. I'm not working with an 'a' tag btw.

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

I'm thankful for every advice.

EDIT: this zip contains Excel files (working with JSZIP Utils)


Solution

  • According to this page in the documentation, it's possible to use the FileSaver polyfill to give your download a custom filename. It only works on modern browsers though, you will need to use feature detection to fall back to the default behaviour.

    See if this works:

    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);
            }
        });