Reputation: 435
So What I am trying to do is that I am simply uploading Files to A particular folder using nodeJS with the help of Formidable Module, using the following reference. File Upload using nodeJS
Now, my file gets uploaded to the Folder which I want, but as I am running the command from the CLI, it gives me an error
Here is the code that I tried :
var http = require('http') ;
var formidable = require('formidable') ;
var fs = require('fs');
http.createServer(function(req, res){
if(req.url == '/fileUpload'){
var form = new formidable.IncomingForm();
form.parse(req, function(err,fields,files){
var oldpath = files.filetoupload.path;
var newpath = 'C://xampp/htdocs/nodejs/upload/' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err){
if(err) throw err;
res.write('File Uploaded and Moved !!');
res.end();
});
res.write('File Uploaded');
res.end();
});
}
else{
res.writeHead(200, {'Content-Type': 'text/html'}) ;
res.write('<form action="fileUpload" method = "post" enctype="multipart/form-data">');
res.write('<input type = "file" name="filetoupload" /><br/>') ;
res.write('<input type="submit" />') ;
res.write('</form>') ;
return res.end() ;
}
}).listen(8080) ;
The Error I am getting is :
> Error [ERR_STREAM_WRITE_AFTER_END]: write after end
> at write_ (_http_outgoing.js:572:17)
> at ServerResponse.write (_http_outgoing.js:567:10)
> at C:\xampp\htdocs\nodejs\upload.js:14:8
> at FSReqWrap.args [as oncomplete] (fs.js:140:20) Emitted 'error' event at:
> at writeAfterEndNT (_http_outgoing.js:634:7)
> at process._tickCallback (internal/process/next_tick.js:63:19)
Upvotes: 0
Views: 1095
Reputation: 208
You are trying to write after you've closed the stream.
var form = new formidable.IncomingForm();
form.parse(req, function(err,fields,files){
var oldpath = files.filetoupload.path;
var newpath = 'C://xampp/htdocs/nodejs/upload/' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err){
if(err) throw err;
res.write('File Uploaded and Moved !!');
res.end(); // here, remove this end call
});
});
Upvotes: 3