w3schools.comTHE WORLD'S LARGEST WEB DEVELOPER SITE Node.js Tutorial Node.js HOME Node.js Intro Node.js Get Started Node.js Modules Node.js HTTP Module Node.js File System Node.js URL Module Node.js NPM Node.js Events Node.js Upload Files Node.js Email Node.js MySQL MySQL Get Started MySQL Create Database MySQL Create Table MySQL Insert Into MySQL Select From MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL Join Node.js MongoDB MongoDB Get Started MongoDB Create Database MongoDB Create Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB Limit MongoDB Join Raspberry Pi RasPi Get Started RasPi GPIO Introduction RasPi Blinking LED RasPi LED & Pushbutton RasPi Flowing LEDs RasPi WebSocket RasPi RGB LED WebSocket RasPi Components Node.js Reference Built-in Modules Node.js Upload Files The Formidable Module There is a very good module for working with file uploads, called "Formidable". The Formidable module can be downloaded and installed using NPM: C:\Users\Your Name>npm install formidable After you have downloaded the Formidable module, you can include the module in any application: var formidable = require('formidable'); Upload Files Now you are ready to make a web page in Node.js that lets the user upload files to your computer: 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('
'); res.write('
'); res.write(''); res.write('
'); 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('
'); res.write('
'); res.write(''); res.write('
'); return res.end(); } }).listen(8080); Step 3: Save 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 third 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 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('
'); res.write('
'); res.write(''); res.write('
'); return res.end(); } }).listen(8080); COLOR PICKER colorpicker HOW TO Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips Slideshow Filter List Sort List SHARE CERTIFICATES HTML CSS JavaScript SQL Python PHP jQuery Bootstrap XML Top Tutorials HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial jQuery Tutorial Java Tutorial C++ Tutorial Top References HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors jQuery Reference Java Reference Angular Reference Top Examples HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples jQuery Examples Java Examples XML Examples Web Certificates HTML Certificate CSS Certificate JavaScript Certificate SQL Certificate Python Certificate jQuery Certificate PHP Certificate Bootstrap Certificate XML Certificate W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2020 by Refsnes Data. All Rights Reserved. Powered by W3.CSS. W3Schools.com