【问题标题】:How to convert base64 stream into string and write into another file?如何将base64流转换为字符串并写入另一个文件?
【发布时间】:2020-03-11 10:57:15
【问题描述】:

我正在从电子邮件正文中获取消息流,并且此流采用 base64 格式我想解码流。我正在使用 npm 模块的“base64-stream”来解码流,但出现错误。

TypeError: base64.decode 不是函数

   function buildAttMessageFunction(attachment) {
  var filename = attachment.params.name;
  var encoding = attachment.encoding;

  return function (msg, seqno) {
    var prefix = '(#' + seqno + ') ';
    msg.on('body', function(stream, info) {
      //Create a write stream so that we can stream the attachment to file;
      console.log(prefix + 'Streaming this attachment to file', filename, info);
      var writeStream = fs.createWriteStream(filename);
      writeStream.on('finish', function() {
        console.log(prefix + 'Done writing to file %s', filename);
      });

      //stream.pipe(writeStream); this would write base64 data to the file.
      //so we decode during streaming using 
      if (toUpper(encoding) === 'BASE64') {
        //the stream is base64 encoded, so here the stream is decode on the fly and piped to the write stream (file)
        stream.pipe(base64.decode()).pipe(writeStream);
      } else  {
        //here we have none or some other decoding streamed directly to the file which renders it useless probably
        stream.pipe(writeStream);
      }
    });
    msg.once('end', function() {
      console.log(prefix + 'Finished attachment %s', filename);
    });
  };
}

【问题讨论】:

    标签: node.js base64


    【解决方案1】:

    我猜你需要用这种方式

    const {Base64Decode} = require("base64-stream");
    stream.pipe(new Base64Decode()).pipe(writeStream);
    
    

    【讨论】:

    • 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多