Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

file uploading node.js

//<html> 
  //<head><title> NodeJS File Upload Example </title></head> 
  //<body>
    //<form action="http://localhost:80/upload" method="post" enctype="multipart/form-data">
    
     // <input type="file" name="fileupload">
      //<br>
      //<input type="submit">
    //</form>
  //</body> 
//</html>


let http = require('http');
let formidable = require('formidable');
let fs = require('fs');

http.createServer(function (req, res) {

  //Create an instance of the form object
  let form = new formidable.IncomingForm();

  //Process the file upload in Node
  form.parse(req, function (error, fields, file) {
    let filepath = file.fileupload.filepath;
    let newpath = 'C:/upload-example/';
    newpath += file.fileupload.originalFilename;

    //Copy the uploaded file to a custom folder
    fs.rename(filepath, newpath, function () {
      //Send a NodeJS file upload confirmation message
      res.write('NodeJS File Upload Success!');
      res.end();
    });
  });

}).listen(80);
 
PREVIOUS NEXT
Tagged: #file #uploading
ADD COMMENT
Topic
Name
1+3 =