Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
 


  

 
Node.js Upload Files
Question Posted on 02 May 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js Upload Files


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);






Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |