从base64转换文件并作为二进制文件发送到某些外部服务

时间:2013-08-13 10:33:27

标签: node.js file rest

我将文件作为base64字符串发送到node.js服务器,然后我需要将其作为二进制文件发送到外部服务。是否可以在不将文件保存到文件系统的情况下执行此操作?现在我正在尝试以这种方式在node.js端获取文件:

var fileData    = req.body.value,
    fileName    = req.body.id,
    base64Data  = fileData.replace(/^data:image\/jpeg;base64,/,"");

modules.fs.writeFile(
     fileName, 
     base64Data, 
     'base64', 
     function(err) 
     {
         if (err)
         {
             console.log(err);
         }
         else
         {
             //read file from the file system and send to the external service 
         }
     });

1 个答案:

答案 0 :(得分:0)

将其写入缓冲区:

var buffer = new Buffer(base64Data, 'base64');

替代方案可能是https://github.com/cjblomqvist/base64Stream