NodeJS membaca file yang dikirim dalam permintaan tanpa menyimpan ke sistem file
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);
});
});
amit.bhagat