I'm a newbie in html and Node.js and trying to upload image to my web app. The problem that I have is it shows me ERR_EMPTY_RESPONSE error after clicking upload button from html.
I think because it is not sending response back from Node.js. So I'm trying to put res.status(501) for error and res.status(200) if it goes through OK.
However, I don't know where I have to put that thing.
Here is my upload button code below,
<form action="/upload", method="post", enctype="multipart/form-data">
<div style='height:0px; width:0px; overflow:hidden;'><input type="file" name="upFile" id="upFile" onchange="getCmaFileView(this, 'name')" target="dropzone_1"/></div>
<div style='height:0px; width:0px; overflow:hidden;'><input type="submit" name="Upload" id="Upload" /></div>
</form>
And here is my app.post code from node.js
app.post('/upload', function(req, res, next) {
var form = new multiparty.Form();
// get field name & value
form.on('field',function(name,value){
console.log('normal field / name = '+name+' , value = '+value);
});
// file upload handling
form.on('part',function(part){
var filename;
var size;
if (part.filename) {
filename = part.filename;
size = part.byteCount;
}else{
part.resume();
}
console.log("Write Streaming file :"+global_username);
var writeStream = fs.createWriteStream('/opt/smarthome/files/'+global_username);
writeStream.filename = filename;
part.pipe(writeStream);
part.on('data',function(chunk){
console.log(global_username+'.png'+' read '+chunk.length + 'bytes');
});
part.on('end',function(){
console.log(global_username+'.png'+' Part read complete');
writeStream.end();
});
});
// all uploads are completed
form.on('close',function(){
console.log('a');
});
// track progress
form.on('progress',function(byteRead,byteExpected){
console.log(' Reading total '+byteRead+'/'+byteExpected);
});
form.on('error',function(){
console.log('error');
if(err != null){
res.status(501);
return;
} else{
res.status(200);
}
});
form.parse(req);
});
As you see above Node.js code, I tried to put res.status at the bottom, but it doesn't work and keep showing me ERR_EMPTY_RESPONSE
Am I thinking wrong?? Or is there better way to do this?
Thanks in advance!
via paulc1111
No comments:
Post a Comment