Currently, I am utilizing the following code for a file upload service:
onFileUploadStart: function (file) {
uploadPath = "";
imageDimensions = null;
console.log(file.originalname + ' is starting to upload...');
},
onFileUploadComplete: function (file) {
uploadPath = file.path;
uploadPath = uploadPath.replace(/\\/g,"/");
console.log("Upload complete. " + file.fieldname + ' uploaded to ' + file.path)
sizeOf(file.path, function (err, dimensions) {
if (err){
console.log("ERROR, Can't get size of image! " + err);
return;
}
else{
imageDimensions = {
width : dimensions.width,
height: dimensions.height
};
console.log("Size of image: width: " + imageDimensions.width + ". height: " + imageDimensions.height);
}
});
}
Also,
app.post('/imageupload',function(req,res){
upload(req,res,function(err) {
console.log("response is being send..");
if(err) {
return res.end("Error uploading file.");
}
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.json({
dimensions: imageDimensions,
path: uploadPath
});
});
});
According to my observation mentioned here, it seems like onFileUploadComplete is processed before the .post handler gets executed. However, when I check my console output, I see:
a.jpg is starting to upload...
Upload complete. file uploaded to uploadeddata\a1235412.jpg
response is being send..
Size of image: width: 2835, height: 1654
This indicates that while in the middle of the onFileUploadComplete function, the .post handler is already running.
Does anyone have suggestions on how I can resolve this issue?