JS: Download zip file from a string with Javascript

javascript blob zip file
zip.js example
javascript download multiple files zip
jszip(); // add files to zip
node js zip multiple files
jszip angularjs
jszip unzip
jszip multiple files

I want to receive a zip file as a string from an Ajax request and then hold it in memory so it can be downloaded multiple times if necessary so that only the one request is sent.

I tried to download it with this:

zip_string = 'PK etc.'

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

// Start file download.
download("zip1.zip", zip_string);

It came through as a zip but then there was obviously a problem because it wouldn't open. Can anyone see what I'm doing wrong?

I solved the problem by encoding the zip file string as base64 on the server before it gets sent through.

with open(file, "rb") as f:
    bytes = f.read()
    encoded = base64.b64encode(bytes)

And then in the JS I just specify that it's base64:

zip_string = 'UEsDBBQAAAAIANQzCU0J56mLPAIAAD4VAAAOAAAA etc.'

function download(filename, data) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;base64,' + data);
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

// Start file download.
download("zip1.zip", zip_string);

Create Zip Files with JavaScript, The data type of your link's href is plain text (ie as specified by data:text/plain ) which basically means, the contents of the link will be treated by  JSZip is a javascript library for creating, reading and editing .zip files, with a lovely and simple API. Current version : v3.2.0 License : JSZip is dual-licensed.

The data type of your link's href is plain text (ie as specified by data:text/plain) which basically means, the contents of the link will be treated by the browser as plain text.

A zip archive is a binary format - you will need to do a bit more work to generate a real zip file and then cache it client side, in this way. You may want to look at zip.js as a library to help you with this.

You can however make a simple change to get the download function working - just change "zip1.zip" to "zip1.txt". I've prepared a jsFiddle here if you to see this in action.

Hope that helps!

JSZip, You can allow users to select and download images from a gallery or just Once the library is available within the page, generating a Zip file is the zip file! read_zip.file("hello.txt").async("string"); // a promise of "Hello World\n" }); There are more advanced and complicated libraries available, like zip.js,  Create .zip files using JavaScript. Provides a simple API to place any content generated by JavaScript into a .zip file for your users.

Using JSZip package: https://stuk.github.io/jszip/

public async downloadFile(filename: string, content: string) {
        const zip = new JSZip();
        await zip.loadAsync(content, {base64: true});
        const blob = await zip.generateAsync({type:"blob"});

        const element = document.createElement("a");
        element.setAttribute("href", window.URL.createObjectURL(blob));
        element.setAttribute("download", filename);
        element.style.display = "none";
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
    }


const fileName = "test.zip";
const content = "UEsDBAoAAAAAAI5ONE9i6ZVwCQAAAAkAAAAIAAAAdGVzdC50eHRUZXN0IGZpbGVQSwECPwAKAAAAAACOTjRPYumVcAkAAAAJAAAACAAkAAAAAAAAACAAAAAAAAAAdGVzdC50eHQKACAAAAAAAAEAGABz76T2f2/VAXPvpPZ/b9UB1NBncjhp1QFQSwUGAAAAAAEAAQBaAAAALwAAAAAA"; // base64 content without mimeType

this.downloadFile(fileName, content);

Download the generated zip file, JSZip is a javascript library for creating, reading and editing .zip files, with a Manually : download JSZip and include the file dist/jszip.js or dist/jszip.min.js. Using a library. Make libraries, not the war. FileSaver.js implements the saveAs() FileSaver interface in browsers that do not natively support it.. If you need to save really large files bigger then the blob's size limitation or don't have enough RAM, then have a look at the more advanced StreamSaver.js that can save data directly to the hard drive asynchronously with the power of the new

JSZip: JavaScript zip class, Create .zip files using JavaScript. Provides a simple Download the generated zip file. The FileSaver API. result; js code; html code click to download. var zip  If you just download the js-zip.zip file, all of the necessary source is there. Here's what the application code looks like in Javascript: // In my demo, this gets attached to a click event. // it instantiates a ZipFile, and provides a callback that is // invoked when the zip is read.

JS: Download zip file from a string with Javascript, Other solution-ish: Blob URL / FileSaver / FileSaver.js. With some recent browsers come a new way to download Blobs (a zip file for example) : blob urls. The download asBinary() , string, the content as binary (utf8 decoded if necessary). Stack Overflow Public questions and yarn add js-file-download npm install --save js-file-download Generate .txt file from string and force download in

Want to download a zip file that's returned as FileStreamResult only , javascript blob zip file jquery download multiple files as zip zip.js example read zip file in javascript axios download zip file javascript download multiple files zip Luckily, failures like this are rare, so zipping files remains a popular option when handling smaller files. An API to zip files is another file transformation provided by the Filestack API. It allows you to zip a single file or multiple files. Generate the Zip File. Filestack’s transformation engine is able to generate zip files.

Comments
  • You should create a blob url instead of creating a base64 str, using URL.createObjectURL(blob)
  • What's the advantage of doing it that way?
  • Thanks, but I want an actual .zip file to be downloadable, not a .txt file