Sails.js Skipper:如何在上传过程中读取上传的文件流? [英] Sails.js Skipper: How to read the uploaded file stream during upload?
问题描述
现在,我不希望文件上传完成,然后我开始在服务器上进一步处理文件。我想读取正在上传的文件流。这里是我如何实现同样的:
$ b $ pre $ var csv = require(fast-csv);
$ b bulk:function(req,res){
function parseEntry(entry){
return entry
}
var inputStream = req.file('employees');
var csvStream = csv();
inputStream.pipe(csvStream);
$ b csvStream.on(data,function(data){
count ++
//跳过无效记录
var parsedData = parseEntry(data);
console.log(parsedData);
});
$ b csvStream.on(end,function(){
// console.log(总分钟数:,timeTaken);
return res.json (200,{status:1,message:成功上传文件});
});
但是我的日志只显示结束事件,没有数据事件正在被捕获。我在文档中读到 req.file(filename)
将返回文件流的流。但是,如何访问我所需的特定文件流,因为我只上传单个文件? 刚刚发表 skipper-csv ;)
npm install skipper-csv --save
使用与使用其他适配器相同的方式。
它使用 csv-parse 来进行实际的解析。您通常会将csv-parse选项传递给上传功能。此外,你传递每个分析行被调用的rowHandler函数。如果您同时上传多个文件,也会传递fd(文件描述符)。这里是一个例子:
$ p $ req.file('files')。upload({
adapter:require(' skipper-csv'),
csvOptions:{delimiter:',',columns:true},
rowHandler:function(row,fd){
console.log(fd,row);
}
},函数(err,files){
if(err)
return res.serverError(err);
return res.json ({
message:Uploaded+ files.length +CSV files!,
files:files
});
});
I am using sails version 0.11 which comes bundled with skipper for file uploads. I need to upload big files of over 100 MB in size at least.
Now I don't want the file upload to complete before I start further processing of the file on server. I want to read the file stream while it is being uploaded. Here is how I am trying to achieve the same:
var csv = require("fast-csv");
bulk: function (req, res){
function parseEntry(entry){
return entry
}
var inputStream = req.file('employees');
var csvStream = csv();
inputStream.pipe(csvStream);
csvStream.on("data", function (data){
count++
// skip invalid records
var parsedData = parseEntry(data);
console.log(parsedData);
});
csvStream.on("end", function(){
// console.log("total time taken in minutes:", timeTaken);
return res.json(200, {"status": 1, "message": "successfully uploaded the file"});
});
}
But my logs only show up the end event and no data event is being captured. I read in the documentation that req.file("filename")
will return stream of file streams. But how do I access the particular file stream I need since I am only uploading a single file?
Just published skipper-csv ;)
npm install skipper-csv --save
Use it the same way you would use other adapters. It uses csv-parse to do the actual parsing. You typically pass csv-parse options to the upload function. Additionally you pass the rowHandler function that is called for each parsed row. The fd (file descriptor) is also passed in case you are uploading multiple files at the same time. Here is an example:
req.file('files').upload({
adapter: require('skipper-csv'),
csvOptions: {delimiter: ',', columns: true},
rowHandler: function(row, fd){
console.log(fd, row);
}
}, function (err, files) {
if (err)
return res.serverError(err);
return res.json({
message: "Uploaded " + files.length + " CSV files!",
files: files
});
});
这篇关于Sails.js Skipper:如何在上传过程中读取上传的文件流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!