Busboy 在读取文件长度和文件数据之前发回响应?我正在上传单个文件
Busboy sending response back before reading file length and file data? i am uploading single file
module.exports.upload = function (req, res) {
fileName = path.basename(req.query.fileName);
var fileExtn = path.extname(req.query.fileName);
console.log("fileName " + fileName);
console.log("fileExtn " + fileExtn);
var fileCheckResponse = FileCheck(req, res); // FileCheck method containing Busboy code
console.log("file check response =" + fileCheckResponse);
}
function FileCheck(req, res) {
var dataSize;
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on("data", function (data) {
dataSize=data.length;
console.log("File [" + fieldname + "] got " + data.length + " bytes");
console.log("the upload file data ===== " + data);
})
});
busboy.on('finish', function() {
console.log('Upload complete');
return dataSize;
});
return req.pipe(busboy);
}`
我在上传方法中调用 FileCheck 函数,其中存在 Busboy 代码,我首先调试并检查 FileCheck 函数,它到达行 ( busboy.on('file', function(fieldname, file, filename, encoding, mimetype) )然后它不会进入它进入另一行( busboy.on('finish', function()) 然后响应对象返回到上传方法,我期待数据长度在 fileCheckResponse 中但获取不包含它的对象,再次继续调试它返回到 FileCheck 函数,这次它命中(file.on("data",函数(数据))并在控制台上打印数据正如我所写,但响应不会返回到上传方法。
我的问题和目标又是获取数据长度作为响应,但现在我第一次获取对象作为响应,第二次指针没有返回到上传方法。
// try using const or let instead of var
module.exports.upload = function (req, res) {
fileName = path.basename(req.query.fileName);
const fileExtn = path.extname(req.query.fileName)
console.log("fileName " + fileName);
console.log("fileExtn " + fileExtn);
// try to make FileCheck function synchronous using promise or async await
FileCheck(req, res)
.then(fileSize => {
console.log(fileSize, 'fileSize')
// send whatever response you need
})
}
function FileCheck(req, res) {
return new Promise((resolve, reject) => {
let dataSize;
let busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on('data', function (data) {
dataSize = data.length;
console.log("File [" + fieldname + "] got " + data.length + " bytes");
})
});
busboy.on('finish', function() {
console.log('Upload complete');
resolve(dataSize)
});
req.pipe(busboy)
})
}
module.exports.upload = function (req, res) {
fileName = path.basename(req.query.fileName);
var fileExtn = path.extname(req.query.fileName);
console.log("fileName " + fileName);
console.log("fileExtn " + fileExtn);
var fileCheckResponse = FileCheck(req, res); // FileCheck method containing Busboy code
console.log("file check response =" + fileCheckResponse);
}
function FileCheck(req, res) {
var dataSize;
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on("data", function (data) {
dataSize=data.length;
console.log("File [" + fieldname + "] got " + data.length + " bytes");
console.log("the upload file data ===== " + data);
})
});
busboy.on('finish', function() {
console.log('Upload complete');
return dataSize;
});
return req.pipe(busboy);
}`
我在上传方法中调用 FileCheck 函数,其中存在 Busboy 代码,我首先调试并检查 FileCheck 函数,它到达行 ( busboy.on('file', function(fieldname, file, filename, encoding, mimetype) )然后它不会进入它进入另一行( busboy.on('finish', function()) 然后响应对象返回到上传方法,我期待数据长度在 fileCheckResponse 中但获取不包含它的对象,再次继续调试它返回到 FileCheck 函数,这次它命中(file.on("data",函数(数据))并在控制台上打印数据正如我所写,但响应不会返回到上传方法。
我的问题和目标又是获取数据长度作为响应,但现在我第一次获取对象作为响应,第二次指针没有返回到上传方法。
// try using const or let instead of var
module.exports.upload = function (req, res) {
fileName = path.basename(req.query.fileName);
const fileExtn = path.extname(req.query.fileName)
console.log("fileName " + fileName);
console.log("fileExtn " + fileExtn);
// try to make FileCheck function synchronous using promise or async await
FileCheck(req, res)
.then(fileSize => {
console.log(fileSize, 'fileSize')
// send whatever response you need
})
}
function FileCheck(req, res) {
return new Promise((resolve, reject) => {
let dataSize;
let busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on('data', function (data) {
dataSize = data.length;
console.log("File [" + fieldname + "] got " + data.length + " bytes");
})
});
busboy.on('finish', function() {
console.log('Upload complete');
resolve(dataSize)
});
req.pipe(busboy)
})
}