如何使用Node JS将Base64转换为HTML

时间:2018-10-29 11:13:40

标签: javascript node.js

  

在这里,我尝试使用猛mm象npm将base64转换为html,但是它抛出错误:-   引发新错误(“找不到中央目录的末尾:这是一个zip文件吗?” +

错误:找不到中央目录的结尾:这是一个zip文件吗?如果是,请参见http://stuk.github.io/jszip/documentation/howto/read_zip.html    在ZipEntries.readEndOfCentral(/Users/Desktop/mommoth/node_modules/jszip/lib/zipEntries.js:149:23)    在ZipEntries.load(/Users/Desktop/mommoth/node_modules/jszip/lib/zipEntries.js:215:14)    在新的ZipEntries(/Users/Desktop/mommoth/node_modules/jszip/lib/zipEntries.js:21:14)    在JSZip.module.exports [作为加载](/Users/Desktop/mommoth/node_modules/jszip/lib/load.js:11:18)    在新的JSZip(/Users/Desktop/mommoth/node_modules/jszip/lib/index.js:39:14)    在Object.openArrayBuffer(/Users/Desktop/mommoth/node_modules/mammoth/lib/zipfile.js:10:19)    在Object.openZip(/Users/Desktop/mommoth/node_modules/mammoth/lib/unzip.js:16:41)    在转换时(/Users/Desktop/mommoth/node_modules/mammoth/lib/index.js:34:18)    在Object.convertToHtml(/Users/Desktop/mommoth/node_modules/mammoth/lib/index.js:22:12)    在/Users/Desktop/mommoth/server.js:49:10    在FSReqWrap.readFileAfterClose上(按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();

});

1 个答案:

答案 0 :(得分:0)

您的节点版本是什么?

#include <stdio.h>
#define LENGTH 5

enum Type {CHAR = 0, INT, DOUBLE};

struct Variant {
    int type;
    union { 
        char c;   
        int i;    
        double d; 
    };
};

void Print(struct Variant v) {
    switch (v.type) {
        default: 
            printf("None"); 
            break;
        case CHAR: 
            printf(" %c", v.c); 
            break;
        case INT: 
            printf(" %d", v.i); 
            break;
        case DOUBLE: 
            printf(" %f", v.d); 
            break;
    }   
}

int main(void) {
    int a = 3;
    float b = 2.5;
    char c = 'H';
    int d = 421;
    char e = 'a';
    struct Variant array[5] = {0};
    array[0].type = INT; 
    array[0].i = a;

    array[1].type = DOUBLE; 
    array[1].d = b;

    array[2].type = CHAR; 
    array[2].c = c;

    array[3].type = INT; 
    array[3].i = d;

    array[4].type = CHAR; 
    array[4].c = e;

    Print(array[rand() % LENGTH]);  
    printf("\n");
}

此方法适用于Node.js v5.11.1及更低版本,如果您的Node.js版本为v6.0.0或更高版本,则应以这种方式进行转换

new Buffer(base64Path, 'base64');