Node JS - Node js upload files - Node - Node JS tutorial - webnode




How to upload the files in Node.js ?

  • Uploading means data is being sent from your computer to the Internet.
  • Examples of uploading include sending email, posting photos on a social media site and using your webcam.
  • Even clicking on a link on a web page sends a tiny data upload.

node.js file upload

Learn Node js - node js Tutorial - Node.js file upload - - node - Node js Examples


The Formidable Module:

  • The Formidable module can be downloaded and installed using NPM:
C:\Users\Your Namenpm install formidable 
  • After you have downloaded the Formidable module, you can include the module in any application:
var formidable = require('formidable');

Upload Files:

 nodejs file upload

Learn Node js - node js Tutorial - nodejs file upload - node - Node js Examples

Step 1: Create an Upload Form

    • Create a Node.js file that writes an HTML form, with an upload field:

Example:

  • This code will produce an HTML form:
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"><br>');
  res.write('<input type="submit">');
  res.write('</form>');
  return res.end();
}).listen(8080);

Step 2: Parse the Uploaded File

    • Include the Formidable module to be able to parse the uploaded file once it reaches the server.
    • When the file is uploaded and parsed, it gets placed on a temporary folder on your computer.

Example

  • The file will be uploaded, and placed on a temporary folder:
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"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);

Step 3: The File

    • When a file is successfully uploaded to the server, it is placed on a temporary folder.
    • The path to this directory can be found in the "files" object, passed as the second argument in the parse() method's callback function.
    • To move the file to the folder of your choice, use the File System module, and rename the Node JS Tutorial File:

Example

  • Include the fs module, and move the file to the current folder:
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:/Users/Your Name/' + files.filetoupload.name;
      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"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);


This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Upload Files

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×