3

我正在使用“express”模块上传文件。我必须使用node-exif读取上传图像的 EXIF 数据。我不想将文件存储在磁盘上,并且上面的模块支持从缓冲区读取 EXIF 数据。我需要从上传的图像中读取缓冲区数据。这是上传代码:

var express = require('express');
var app = express();
var fs = require('fs');
var multiparty = require("multiparty");

module.exports.UploadImage = function (req, res, next) {
    // Handle request as multipart  
    if (!req.files)
        return res.status(400).send('No files were uploaded.');  

    var sampleFile = req.files.uploadedFile;
    //Here I need to have the buffer.
    res.send('Done!');
}

有人可以帮我获取缓冲区数据,因为我对 Node 世界很陌生吗?

4

2 回答 2

1

以下是获取缓冲区的方法:

var express = require("express");
const fileUpload = require('express-fileupload');
var app = express();
app.use(fileUpload());

app.post("/upload", (req, res) => {

   console.log(req.files.file);

   res.status(200).send('Success!!!');
});

控制台输出:

{
  name: 'task.txt',
  data: <Buffer ef bb bf 37 38 37 39 34 38 36 34 0d 0a 37 38 37 39 ... 57 more bytes>,
  size: 107,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'text/plain',
  md5: '6e37e5195b2acfcac7713952ba080095',
  mv: [Function: mv]
}

数据参数是您所需要的。您可以将缓冲区解析为字符串,如下所示:

   console.log(req.files.file.data.toString('utf8'));
于 2020-04-27T06:28:17.027 回答
0

我想这就是你要找的

  module.exports.UploadImage = function (req, res, next) {
    // Handle request as multipart  
    if (!req.files)
        return res.status(400).send('No files were uploaded.');  

    var sampleFile = req.files.uploadedFile;
    //Here I need to have the buffer.
    var chunks = []
    req.on('data', function (chunk) {
        // reads chunks of data in Buffer
        console.log(chunk) // Prints <Buffer 8a 83 ef ... >
        chunks.push(chunk)
    })

    res.send('Done!');
  }
于 2017-08-20T15:56:06.613 回答