我想使用上传的’file.txt’,使用multer和逐行模块.使用multer上传文件后,我尝试检查数据,我有一个缓冲区对象,我无法弄清楚如何访问数据并使用逐行模块进行处理. 这是我的代码: var e
这是我的代码:
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 + '').split('\n')