jszip
jszip copied to clipboard
blob is not supported by this platform
The below error i am getting from salesorce ligthing component and the code is below
({
saveData : function(data, fileName, helper) {
var attachments = data.attachments;
var zip = new JSZip();
var csv = helper.convertArrayOfObjectsToCSV(data.contentDocumentLinks);
alert(data.contentDocumentLinks);
zip.file("Attachments.csv", csv);
var attachmentsFolder = zip.folder("Attachments");
for(var i=0;i<attachments.length;i++){
attachmentsFolder.file(attachments[i].docId + '_' +attachments[i].title + '.' + attachments[i].ext, attachments[i].data, {base64: true});
}
zip.generateAsync({type:"blob"}).then(function(content) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var url = window.URL.createObjectURL(content);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
$A.get("e.force:closeQuickAction").fire()
});
},
convertArrayOfObjectsToCSV : function(objectRecords){
var csvStringResult, counter, keys, columnDivider, lineDivider;
if (objectRecords == null || !objectRecords.length) {
return null;
}
columnDivider = ',';
lineDivider = '\n';
var keystring = ['RecordId' ,'AttachmentId'];
keys = ['LinkedEntityId' ,'ContentDocumentId'];
csvStringResult = '';
csvStringResult += keystring.join(columnDivider);
csvStringResult += lineDivider;
for(var i=0; i < objectRecords.length; i++){
counter = 0;
for(var sTempkey in keys) {
var skey = keys[sTempkey] ;
// add , [comma] after every String value,. [except first]
if(counter > 0){
csvStringResult += columnDivider;
}
csvStringResult += '"'+ objectRecords[i][skey]+'"';
counter++;
} // inner for loop close
csvStringResult += lineDivider;
}// outer main for loop close
// return the CSV formate String
return csvStringResult;
},
})
use three ` when posting code
what engine are you trying this on? node? browser?
I am getting the same error. I am using firebase cloud functions Node js. What I want to do is I want to zip all the files in firebase storage and send the link in email when the user click on link, zip file will be downloaded. Please guide asap. Thanks!
I got the same error when I ran the following code. i'm using macOS Catalina 10.15.6 using Node.js 14.7.0:
const JSZip = require('jszip');
const main = async () => {
const zip = new JSZip();
await zip.file('hello.txt', "Hello World\n");
const content = await zip.generateAsync({ type: 'string' });
console.log('content: ', content);
}
main()
.catch(console.error)
.finally(() => process.exit());
output:
$ node zip.js
Error: blob is not supported by this platform
at Object.exports.checkSupport (/Users/ls/code/src/ltfschoen/ethquad/node_modules/jszip/lib/utils.js:352:15)
at JSZip.generateInternalStream (/Users/ls/code/src/ltfschoen/ethquad/node_modules/jszip/lib/object.js:347:17)
at JSZip.generateAsync (/Users/ls/code/src/ltfschoen/ethquad/node_modules/jszip/lib/object.js:375:21)
at main (/Users/ls/code/src/ltfschoen/ethquad/scripts/zip.js:21:29)
if i change it from 'blob'
to 'string'
then it outputs:
$ node zip.js
content: PK
VQãå°
hello.txtHello World
PK
VQãå°
hello.txtPK73
@ltfschoen Blob isn't supported in node. In a node env you better use ArrayBuffer.
Alternetive you could use fetch-blob and add it globally before you import jszip
globalThis.Blob = require('fetch-blob');
const JSZip = require('jszip');
Node supports Blob as of v16
https://nodejs.org/dist/latest-v16.x/docs/api/buffer.html#class-blob
Unfortunately it is not on globalThis so will requires some additional effort to get working.
Here is a work-around until JSZip is updated to support Blobs on Node correctly
const buffer = require("buffer");
globalThis.Blob = buffer.Blob;
JSZip.support.blob = true;
That works, but I get the following error if I do it:
TS2322: Type 'typeof Blob' is not assignable to type '{ new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob; prototype: Blob; }'. Types of property 'prototype' are incompatible. Property 'prototype' is missing in type 'import("buffer").Blob' but required in type 'Blob'.
This was working for me in a TypeScript project when I put it before importing JSZip:
import buffer from 'node:buffer';
(globalThis as any).Blob = buffer.Blob;
@zuani Thanks, that's what I ended up doing, but casting to any
is of course bad practice.