node.js - 如何将图像写入具有缓冲区的服务器?

标签 node.js buffer node.js-stream

在 Expressjs 中,我上传图像。它以 7 位编码的缓冲区形式出现:

 { fieldname: 'file',
  originalname: 'img.JPG',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 64 00 64 00 00 ff db 00 43 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 0
1 01 01 01 01 01 ... >,
  size: 873066 }

如何将其作为图像保存到文件系统?某些内容已保存,但不是图像:

Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted of too large.

这些不起作用:

fs.writeFile('uploaded-img.jpg', req.file, 'ascii', function(err) {

fs.writeFile('uploaded-img.jpg', req.file.toString('ascii'), 'ascii', function(err) {

最佳答案

怎么样:

fs.writeFile('uploaded-img.jpg', req.file.buffer, 'ascii', function(err) {

关于node.js - 如何将图像写入具有缓冲区的服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381009/

相关文章:

node.js - 单流多次消费

stream - pipe()将如何在节点js中执行读写操作?

json - Express:阻止bodyParser解析请求

node.js - Nodejs 全局中间件

c++ - 使用流的字符串连接返回垃圾

解析字符缓冲区的代码

c++ - 数据长度过大的解压缓冲区会崩溃

node.js - 通过stream.end()后重新打开写入流

node.js - 如何处理 Node.js 中的循环依赖

node.js - 在 OpenShift 上运行 npm install --production