Node.js Upload Files
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.
After installing formidable module, you can check your version in command prompt
After downloaded the Formidable module, include the module in any application:
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:
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.
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:
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
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

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

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.