NodeJS writeStream空文件

我正在尝试使用nodeJS来保存存储在base64string中的已处理图像。

var buff = new Buffer(base64data,'base64'); console.log(base64data); var stream = fs.createWriteStream('/path/to/thefile.png'); stream.write(buff) stream.end() 

但是,生成的文件是空的。

当我输出console.log(base64data); 并在本地解码,它会产生一个有效的PNG二进制文件,为什么这个文件是空的?

该文件是一个3600×4800像素的PNG文件(即它是巨大的),这可能是一个因素?

另外,我也尝试写文件,没有运气。

是的, fsrequire('fs')

谢谢

你的stream.end()没有写任何东西。 这是asynchronousfunction记住。

 var buff = new Buffer(base64data,'base64'); console.log(base64data); var stream = fs.createWriteStream('/path/to/thefile.png'); stream.write(buff); stream.on("end", function() { stream.end(); }); 

更好:

 var buff = new Buffer(base64data,'base64'); console.log(base64data); var stream = fs.createWriteStream('/path/to/thefile.png'); stream.write(buff); stream.end(); stream.on('finish', () => { //'All writes are now complete.' }); stream.on('error', (error) => {...});