Node.js Upload Files
Node.js Upload Files
To handle upload files in Node.js we use Formidable Module and that module is used to handle the files. And before use of this we need to install the formidable module and to install that we use below command in Node.js command prompt.
npm install formidable
One you have downloaded the formidable module, we need to include the module by using the below command:-
var formidable=require('formidable')
Upload Files
Here in this tutorial we have used 3 example of file upload syntax or code:-
(1)Upload Files:-Create an upload form
Here we create a Node.js file which we used to writes an HTML form, with an upload filed. Below is the example to create upload file control but not upload the file as per given example:-
var http = require('http');
http.createServer(function (req, res) {
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">
');
res.write('< input type="submit">');
res.write('< /form>');
return res.end();
}).listen(8080);
(2)Upload Files:-Parse the uploaded file
Here we inlcude Formidable module which unable to parse the uploaded file once this will reaches the server.
And once we file uploaded and parse, this will be placed on the temporary folder on your computer. And below is the example and code and syntax:-
var http = require('http');
var formidable = require('formidable');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
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">
');
res.write('< input type="submit">');
res.write('< /form>');
return res.end();
}
}).listen(8080);
(3)Upload Files:-Save the uploaded file
When file is uploaded and placed on a temporary folder. The path to this directory can be found in the "files" object, passed as the third argument in the parse() method's callback function.
And to move the file to particular folder of choice the we can use the File system module, and then we can rename the file. Below is the code and syntax to do that:-
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.filepath;
var newpath = 'C:/Users/ait/' + files.filetoupload.originalFilename;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
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">
');
res.write('< input type="submit">');
res.write('');
return res.end();
}
}).listen(8080);