Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JSZip can't create blob file?

Tags:

node.js

jszip

I was trying JSZip on NodeJS to create some ZIP file but i'm facing an issue.

I'm using the code proposed by JSZip :

var JSZip = require('JSZip')
var zip = new JSZip()
zip.file("Hello.txt", "Hello World\n")
zip.generateAsync({type:"blob"}).then(function(content) {
    //do things here
});

Currently the code throw an error on generateAsync

UnhandledPromiseRejectionWarning: Error: blob is not supported by this platform

Did something need to be install or the data I set in zip.file should be in a certain format ?

like image 704
Alex Avatar asked Dec 31 '22 21:12

Alex


1 Answers

JSZip throws this error at jszip/lib/utils.js:352:15 because of the value of support.blob, defined by at jszip/lib/support.js (lines 11 to 32). I don't know for your machine, but while trying to run this script in NodeJS, I've debugged it to the conclusion that JSZip detects blobs to be not supported because self is not defined, so line 23 throws an error and support.blob is set to false.

JSZip seems to support the Node Buffer type on Node though - the following doesn't throw any errors on my machine:

// JSZip v3.2.1
// NodeJS v8.10.0
// Ubuntu 18.04
const JSZip = require('jszip');

const zip = new JSZip();
zip.file('hello.txt', 'Hello world\n');
zip
    .generateAsync({type: 'nodebuffer'}) // blob -> nodebuffer
    .then(console.log);
like image 146
Nino Filiu Avatar answered Jan 03 '23 10:01

Nino Filiu