connect-busboy 抓不到文件场景
connect-busboy catch no file scenario
我正在使用 connect-busboy
上传电子邮件附件文件。如果存在一个文件,代码工作正常。但是,我想捕捉没有文件 attached/uploaded 的场景。
最初我以为我会检查文件大小是否为零,但后来我意识到 busboy.on('file') 本身没有被触发。
如何检查是否没有文件上传并继续下一步?
代码如下:
if (req.busboy) {
req.busboy.on('field', function (fieldname, value) {
console.log('Field [' + fieldname + ']: value: ' + value);
// collecting email sending details here in field
});
var now = (new Date).getTime();
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
var attachmentfile = '/tmp/' + now + '.' + filename;
fstream = fs.createWriteStream(attachmentfile);
file.pipe(fstream);
fstream.on('close', function () {
console.log("Upload Finished of " + filename);
console.log('Time to upload: ' + utility.getFormattedTime((new Date).getTime() - now));
attachment.file = { 'name': filename, 'location': attachmentfile };
// send email code
return res.send('email sent successfully');
});
});
req.busboy.on('finish', function () {
// validating if input from reading field values are correct or not
});
} else {
res.error('No file attached');
}
我在没有文件的情况下用于测试的 curl 命令是:
curl -X POST \
http://localhost:3000/email/ \
-H 'Cache-Control: no-cache' \
-H 'content-type: multipart/form-data;' \
-F 'data={'some json object' : 'json value'}'
如果我在上面的 curl 命令中添加 -F 'file=@location'
,代码工作正常。
我错过了什么?
如果有文件,您可以使用设置为 true 的变量。
if (req.busboy) {
var fileUploaded = false;
req.busboy.on('field', function (fieldname, value) {
...
});
var now = (new Date).getTime();
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
fileUploaded = true;
...
});
req.busboy.on('finish', function () {
if (!fileUploaded) {
res.error('No file attached');
} else {
// ----- a file has been uploaded
}
});
} else {
res.error('No file attached');
}
我正在使用 connect-busboy
上传电子邮件附件文件。如果存在一个文件,代码工作正常。但是,我想捕捉没有文件 attached/uploaded 的场景。
最初我以为我会检查文件大小是否为零,但后来我意识到 busboy.on('file') 本身没有被触发。
如何检查是否没有文件上传并继续下一步?
代码如下:
if (req.busboy) {
req.busboy.on('field', function (fieldname, value) {
console.log('Field [' + fieldname + ']: value: ' + value);
// collecting email sending details here in field
});
var now = (new Date).getTime();
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
var attachmentfile = '/tmp/' + now + '.' + filename;
fstream = fs.createWriteStream(attachmentfile);
file.pipe(fstream);
fstream.on('close', function () {
console.log("Upload Finished of " + filename);
console.log('Time to upload: ' + utility.getFormattedTime((new Date).getTime() - now));
attachment.file = { 'name': filename, 'location': attachmentfile };
// send email code
return res.send('email sent successfully');
});
});
req.busboy.on('finish', function () {
// validating if input from reading field values are correct or not
});
} else {
res.error('No file attached');
}
我在没有文件的情况下用于测试的 curl 命令是:
curl -X POST \
http://localhost:3000/email/ \
-H 'Cache-Control: no-cache' \
-H 'content-type: multipart/form-data;' \
-F 'data={'some json object' : 'json value'}'
如果我在上面的 curl 命令中添加 -F 'file=@location'
,代码工作正常。
我错过了什么?
如果有文件,您可以使用设置为 true 的变量。
if (req.busboy) {
var fileUploaded = false;
req.busboy.on('field', function (fieldname, value) {
...
});
var now = (new Date).getTime();
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
fileUploaded = true;
...
});
req.busboy.on('finish', function () {
if (!fileUploaded) {
res.error('No file attached');
} else {
// ----- a file has been uploaded
}
});
} else {
res.error('No file attached');
}