我想使用上传的 'file.txt' ,使用 multer 和 line-by-line 模块。用 multer 上传文件后,我试图检查数据,我得到了一个缓冲区对象,但我不知道如何访问数据并使用逐行模块处理它。 这是我的代码:
var express = require("express"),
fs = require('fs'),
app = express(),
bodyParser = require("body-parser"),
multer = require('multer'),
upload = multer({
dest: 'uploads/',
inMemory:true,
onFileUploadData: function (file, data) {
console.log(data.length + ' of ' + file.fieldname + ' arrived')
}
}),
LineByLineReader = require('line-by-line'),
path = require('path'),
ejs = require("ejs"),
Excel= require("exceljs");
app.engine('html', ejs.renderFile);
app.set('view engine', 'html');
app.use("/static", express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/GffData", upload.single('file'), function (req, res) {
console.log("this is the request : ", req.file);
fs.readFile(req.file.path, function (err, data) {
if (err) throw err;
// data will contain your file contents
console.log("the data is : ",data)
generateObjectFromGff(data); //this is the function that will use line by line module
});
res.send("done")
})
当我在控制台上看到数据时,我得到一个缓冲区:
<Buffer 4c 6d 6a 46 2e 30 31 09 54 72 69 54 72 79 70 44 42 09 43 44 53 09 33 37 30 34 09 34 37 30 32 09 2e 09 2d 09 30 09 22 49 44 3d 63 64 73 5f 4c 6d 6a 46 ... >
而且逐行模块不能继续处理这个。 你能帮我弄清楚吗?
最佳答案
如果您的 data
变量是 Buffer
对象,您可以通过添加空字符串将其轻松转换为字符串:data + ''
。
如果您想逐行访问您的文本,您可以将其转换为行数组:
(data + '').split('\n')
关于node.js - 访问 Multer 缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438918/