Vote count:
0
I have a simple form, that sends multipart POST data to my Node.JS(v0.10.26) server, with Express(v3.1.2). This is the form:
<form id = "uploadForm"
enctype = "multipart/form-data"
action = "/upload"
method = "post">
<input type="file" name="userFile" />
<input type="submit" value="Upload File" name="submit">
</form>
On the server side, I am supposed to stream this file over to an S3 bucket. But before I do, I need to make sure that the bucket does not contain a file with the same name. So, I am getting a list of files present in the bucket and checking if a file with the same name exists.
handleUpload = function(req,res){
console.log('Upload request.');
var client = knox.createClient(s3AccessDetails);
client.list({},function(err,fileListData){
if(err){
/* Handle error. */
}
else{
if( fileListData.Contents.length == 0 ){
fileListData = null;
}
console.log('Calling streamFile.');
streamFile(req, res, fileListData, client);
}
});
}
I am over-riding the req.form.onPart in streamFile. streamFile is defined here.
var streamFile = function(req, res, fileListData, client){
var i = 0;
var errorObj = {};
req.form.on('end',function(){
console.log('Received end.');
/*Handle end.*/
});
req.form.on('error',function(){
console.log('Received error.');
/*Handle error.*/
});
req.form.onPart = function(part){
console.log('Received part.');
if( part.filename != undefined ){
console.log('On part.');
if( fileListData != null ){
/*Code to check if it is a duplicate file name.*/
/*Iterate through fileListData and compare with part.filename.*/
/*Handle a duplicate with appropriate error.*/
}
/* Else continue with the file streaming. */
var headers = {};
headers['Content-Length'] = req.headers['content-length'];
console.log('Headers ' + JSON.stringify(req.headers));
client.putStream(part, part.filename,headers,function(err,uploadresp){
if(err){
console.log('Upload to s3 returned error. ' + err);
/* Handle error. */
}
else{
if( uploadresp == null || uploadresp == undefined){
console.log('Upload response is null.');
/* Handle error. */
}
console.log('S3 response code ' + uploadresp.statusCode);
res.statusCode = uploadresp.statusCode;
uploadresp.pipe(res);
}
})
}
}
req.form.parse(req, function(error){
console.log('In parse.');
if(error){
console.log('Parse error ' + error);
/* Handle error. */
}
});
}
The problem I am facing is, the debug "console.log('Received part.');" is never seen! I tried this using files of size 1KB, 1MB and 15MB.
If I remove the call to client.list and call streamFile directly, this is working. But I need it to work in the callback for client.list. I'm using Knox(v0.9.2) to communicate with the S3 bucket.
This is how I have defined the middlwares in my express app.
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart({ defer: true }));
app.use(express.methodOverride());
app.use(app.router);
This is my first question, apologies if I've gone wrong somewhere. Any leads appreciated!
req.form.onPart not called when defined in a callback function
Aucun commentaire:
Enregistrer un commentaire