简体   繁体   中英

How to convert Base64 into HTML using node js

Here i tried converting the base64 to html using the Mammoth npm, but it is throwing an error: - throw new Error(“Can't find end of central directory : is this a zip file ? ” +

Error: Can't find end of central directory : is this a zip file ? If it is, see http://stuk.github.io/jszip/documentation/howto/read_zip.html at ZipEntries.readEndOfCentral (/Users/Desktop/mommoth/node_modules/jszip/lib/zipEntries.js:149:23) at ZipEntries.load (/Users/Desktop/mommoth/node_modules/jszip/lib/zipEntries.js:215:14) at new ZipEntries (/Users/Desktop/mommoth/node_modules/jszip/lib/zipEntries.js:21:14) at JSZip.module.exports [as load] (/Users/Desktop/mommoth/node_modules/jszip/lib/load.js:11:18) at new JSZip (/Users/Desktop/mommoth/node_modules/jszip/lib/index.js:39:14) at Object.openArrayBuffer (/Users/Desktop/mommoth/node_modules/mammoth/lib/zipfile.js:10:19) at Object.openZip (/Users/Desktop/mommoth/node_modules/mammoth/lib/unzip.js:16:41) at convert (/Users/Desktop/mommoth/node_modules/mammoth/lib/index.js:34:18) at Object.convertToHtml (/Users/Desktop/mommoth/node_modules/mammoth/lib/index.js:22:12) at /Users/Desktop/mommoth/server.js:49:10 at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

let base64String = 'data:text;base64,TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=';

let base64Path = base64String.split(';base64,').pop();

let buff = new Buffer(base64Path, 'Base64');

console.log(buff);

mammoth.convertToHtml({ buffer :  buff })                
 .then(function(error,result){

    if(error){console.error(error)}

   else{

       console.log('convert');              
       console.log(result);
   }

  })

.done();

});

What is your node version?

new Buffer(base64Path, 'base64');

This method is for Node.js v5.11.1 and below, if your Node.js version is v6.0.0 or above, you should convert in this way

let buff = Buffer.from(base64Path, 'base64');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM