AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js Upload Files


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js Upload Files

How to Upload Files using formidable module?

Formidable is a good module Node.js used for parsing form data, especially file uploads.

Installation of formidable module:

You can download and installed this package using NPM then by using this command.

E:\Users\Your_Name>npm install formidable

After installing formidable module, you can check your version in command prompt

E:\Users\Your_Name>npm version formidable

After downloaded the Formidable module, include the module in any application:

var formidable = require('formidable');

 

Upload Files

After the steps above you are ready to make a web page in Node.js that lets the user upload files to your computer:

1. Create an Upload Form

Create a Node js file that is written in HTML code form with a load field:

Example: This code will produce an HTML form:
var http = require('http');

http.createServer(function  (req, res) {
  res.write_Head(200, {'Content-Type': 'text/html'});
  res.write('<form action="file_upload" method="post" enctype="multipart/form-data">');
  res.write('<input type="file" name="file_to_upload"><br>');
  res.write('<input type="submit">');
  res.write('</form>');
   return res.end();
}).listen(8888);

 

Related subjects:
node.js http module node.js modules global objects nodejs

 

Tags: node js express file upload, node js formdata file upload, node js upload file to another server,
multer nodejs, multer file upload, file upload without multer, formidable nodejs,
upload image to mongodb, upload file to google drive, to firebase storage,
base64, to server express, from url

 

Parse the Uploaded File

2. Parse the Uploaded File.

After the file already uploaded and parsed, this gets placed on a temporary folder on your computer.

Example: file will be uploaded, then placed on a temporary folder:
var http = require('http');
var formidable = require('formidable');

http.createServer(function (req, res) {
if (req.url == '/file_upload') {
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="file_upload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="file_to_upload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8888);

 

Save the File

3. Save the File

After the file successfully uploaded to the server, then it is placed on a temporary folder.

The path to this folder can be found in the "files" object

You can move the file to the folder of your choice on server, use File System module, and rename how you like the file:

Example: Include fs module, 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 == '/file_upload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var old_path = files.file_to_upload.file_path;
var new_path = 'E:/Users/Your_Name/'
+ files.file_to_upload.original_Filename;
fs.rename(old_path, new_path, 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="file_upload" method="post"
enctype="multipart/form-data">');
res.write('<input type="file"
name="file_to_upload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8888);

 



node js express file upload, node js formdata file upload, node js upload file to another server,
multer nodejs, multer file upload, file upload without multer, formidable nodejs,
upload image to mongodb, upload file to google drive, to firebase storage,
base64, to server express, from url

Node.js Upload Files - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...