在保存文件(node.js,express)结束之前,busboy-connect连接完成, [英] busboy-connect fires on finish before the end of save file (node.js , express)
问题描述
我使用busboy connect从我的客户端获取上传数据。我尝试保存数据,然后on.finish返回到服务器的状态。问题是on.finish在文件保存结束之前触发。
我做错了什么,或者是模块的工作方式?
服务器端代码:
console.log(upload started dirname,__dirname);
var fstream;
var result = [];
var number_of_files = 1;
req.pipe(req.busboy);
req.busboy.on('field',function(fieldname,val){
field_obj = {}
field_obj [fieldname] = val;
result.push(field_obj) ;
console.log(field_obj)
});
req.busboy.on('file',function(fieldname,file,filename){
console.log(Uploading:+ filename);
//图像的路径上传
if(result.length> 0){
var file_type = filename.substr(filename.length - 4);
filename = result [0] .name +'_'+ file_type;
number_of_files ++;
}
fstream = fs.createWriteStream(__ dirname +/+ filename);
file.pipe(fstream);
fstream .on('close',function(){
console.log(Upload Finished of+ filename);
result.push(filename);
console.log(result ,结果)
});
});
req.busboy.on('finish',function(){
console.log(form parsing finished)
console.log(result finish,result)
res.sendStatus(200)
});
我期望结果完成是我看到的最后一个msg,但是我得到以下在我的控制台上:
上传:testSwf1.swf
上传:testSwf3.swf
表单解析完成
结果完成[{name:'123'},{clickUrl:'234234'}]
上传已完成123_1.swf
结果[{name:'123'},{clickUrl:' 234234'},'123_1.swf']
上传已完成123_2.swf
结果[{name:'123'},
{clickUrl:'234234'},
'123_1.swf',
'123_2.swf']
编辑
i也试过on.end,但是没有发现任何东西。
req.busboy .on('end',function(){
console.log(DONE PARSING FORM)
console.log(NEW result finish,result)
});
由于mscdex建议我使用变量来查找写作完成:
var fstream;
var result = [];
var number_of_files = 1;
var counter = 0;
req.pipe(req.busboy);
req.busboy.on('field',function(fieldname,val){
field_obj = {}
field_obj [fieldname] = val;
result.push(field_obj) ;
console.log(field_obj)
});
req.busboy.on('file',function(fieldname,file,filename){
counter ++;
console.log(Uploading:+ filename);
/ /图像将被上传的路径
if(result.length> 0){
var file_type = filename.substr(filename.length - 4);
filename = result [0]。 ($ _ $ d $$);
file_type;
file_type;
file.pipe(fstream) ;
fstream.on('close',function(){
counter_;
console.log(Upload Finished+ filename);
result.push(文件名);
console.log(result,result)
if(counter == 0){
console.log(writing finished);
res.sendStatus (200);
}
});
});
I use busboy connect to get the upload data from my client. I try to save the data and then on.finish to return status ok to the server. The problem is that the on.finish fires before the end of the file saving. Have I done something wrong or that's the way the module works?
server-side code :
console.log("upload started dirname ", __dirname);
var fstream;
var result = [];
var number_of_files = 1;
req.pipe(req.busboy);
req.busboy.on('field', function(fieldname, val) {
field_obj = {}
field_obj[fieldname] = val;
result.push(field_obj);
console.log(field_obj)
});
req.busboy.on('file', function(fieldname, file, filename) {
console.log("Uploading: " + filename);
//Path where image will be uploaded
if (result.length > 0) {
var file_type = filename.substr(filename.length - 4);
filename = result[0].name + '_' + number_of_files + file_type;
number_of_files++;
}
fstream = fs.createWriteStream(__dirname + "/" + filename);
file.pipe(fstream);
fstream.on('close', function() {
console.log("Upload Finished of " + filename);
result.push(filename);
console.log("result ",result)
});
});
req.busboy.on('finish', function() {
console.log("form parsing finished")
console.log("result finish",result)
res.sendStatus(200)
});
I would expect the "result finish" to be the last msg i see , but i get the following on my console:
Uploading: testSwf1.swf
Uploading: testSwf3.swf
form parsing finished
result finish [ { name: '123' }, { clickUrl: '234234' } ]
Upload Finished of 123_1.swf
result [ { name: '123' }, { clickUrl: '234234' }, '123_1.swf' ]
Upload Finished of 123_2.swf
result [ { name: '123' },
{ clickUrl: '234234' },
'123_1.swf',
'123_2.swf' ]
EDIT i also tried the on.end but that one didn't fired at all.
req.busboy.on('end', function() {
console.log("DONE PARSING FORM")
console.log("NEW result finish", result)
});
As mscdex suggested I used a variable to find when the writing is finised:
var fstream;
var result = [];
var number_of_files = 1;
var counter = 0;
req.pipe(req.busboy);
req.busboy.on('field', function(fieldname, val) {
field_obj = {}
field_obj[fieldname] = val;
result.push(field_obj);
console.log(field_obj)
});
req.busboy.on('file', function(fieldname, file, filename) {
counter++;
console.log("Uploading: " + filename);
//Path where image will be uploaded
if (result.length > 0) {
var file_type = filename.substr(filename.length - 4);
filename = result[0].name + '_' + number_of_files + file_type;
number_of_files++;
}
fstream = fs.createWriteStream(__dirname + "/" + filename);
file.pipe(fstream);
fstream.on('close', function() {
counter--;
console.log("Upload Finished of " + filename);
result.push(filename);
console.log("result ",result)
if(counter == 0){
console.log("writing finished");
res.sendStatus(200);
}
});
});
这篇关于在保存文件(node.js,express)结束之前,busboy-connect连接完成,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!