📌  相关文章
📜  nodejs读取请求中发送的文件而不保存到文件系统 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:01:08.653000             🧑  作者: Mango

代码示例1
const multer = require('multer');
const {Duplex} = require('stream'); // It's a Native Node Module 
const fs = require('fs');
const readXlsxFile = require('read-excel-file/node')

const storage = multer.memoryStorage();
const upload = multer({
   storage: storage
});

function bufferToStream(bufferIn) {
    let duplex = new Duplex();
    duplex.push(bufferIn);
    duplex.push(null);
    return duplex;
}

app.post('/api/endpoint_name', upload.single('file'), function(req, res) {
    const file_buffer = req.file['buffer'];
    const file_stream = bufferToStream(file_buffer);
    const read_stream = fs.createReadStream(file_stream);
      readXlsxFile(read_stream)
      .then(function(rows) {
          console.log('Rows in the Excel : ', rows);
      });
});