jszip icon indicating copy to clipboard operation
jszip copied to clipboard

blob is not supported by this platform

Open VJ555 opened this issue 6 years ago • 8 comments

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

})

VJ555 avatar Sep 20 '18 08:09 VJ555

use three ` when posting code

skarmavbild 2018-09-14 kl 18 45 31

what engine are you trying this on? node? browser?

jimmywarting avatar Sep 24 '18 13:09 jimmywarting

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!

haris-crewlogix avatar Mar 20 '19 08:03 haris-crewlogix

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 avatar Aug 04 '20 10:08 ltfschoen

@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');

jimmywarting avatar Aug 04 '20 11:08 jimmywarting

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;

matthewjosephtaylor avatar Nov 08 '21 20:11 matthewjosephtaylor

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'.

raarts avatar Dec 08 '22 22:12 raarts

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;

zauni avatar May 05 '23 14:05 zauni

@zuani Thanks, that's what I ended up doing, but casting to any is of course bad practice.

raarts avatar May 06 '23 15:05 raarts