After consulting the data for a period of time, I found that the uploading methods are:1.express middleware multer module 2.connect-multiparty module (but currently not officially recommended) 3.implementation using multiparty module 4.implementation using formidable plugin,This article introduces nodejs + express (4.x +) to implement file upload function.

nodejs is a young programming framework,Full of energy and infinite passion,Fast updates have been maintained.The official web development library express based on nodejs is also developing simultaneously.Upgrade one major version each year,Even major operations were performed on the bottom frame.In express4, the middleware library connect was replaced, and multiple more fine-grained libraries were used instead.The benefits are obvious,These middleware can be updated and released more freely,Will not be affected by the express release cycle;But the problem is also very difficult,Not compatible with previous versions,Upgrading means modifying the code.

After consulting the information and exploring for a period of time, I found that the uploading methods are:1.express middleware multer module (this has the highest efficiency,Natively supported in express3.x,Express4.x has become a separate module), 2.connect-multiparty module (but currently not officially recommended), 3. using multiparty module to implement (this method is more common), 4. using formidable plugin to achieve (the plugin is simple Easy to understand);

The easiest way is to upload via the "connect-multiparty" middleware.

Install by npm install connect-multiparty in the project.


var multipart=require ("connect-multiparty");
var multipartmiddleware=multipart ();
app.post ("/upload", multipartmiddleware, function (req, resp) {
 console.log (req.body, req.files);
 //don "t forget to delete all req.files when done

After uploading, the uploaded file will generate a temporary file in the temporary directory,You can print req.files to see the specific file path.

Just move the temporary file in the commented place and rename it to the actual directory to complete the upload function.


Official address:

But the official does not recommend using this middleware,It is recommended to use "multiparty" directly, because error handling is more troublesome.

Let's use "multiparty" to implement a version.

1. Create a project using express (version 4.11.x),Use the default jade as the template engine.

2. In the project directory,Install necessary components via npm install multiparty.

3. Modify views/index.jade and make a simple form for file upload as follows.

extends layout
  block content form (method="post", action="/file/uploading", enctype="multipart/form-data")
    input (name="inputfile", type="file", multiple="mutiple")
    input (name="btnup", type="submit", value="upload")

4. Modify routes/index.js to implement the background code for uploading pages and uploading responses.

var express=require ("express");
  var router=express.router ();
  var multiparty=require ("multiparty");
  var util=require ("util");
  var fs=require ("fs");
  /* Upload page * /
  router.get ("/", function (req, res, next) {
   res.render ("index", {title:"express"});
 /* Upload * /
 router.post ("/file/uploading", function (req, res, next) {
  //Generate a multiparty object and configure the upload target path
  var form=new multiparty.form ({uploaddir:"./public/files/"});
  //Processing after upload is complete
  form.parse (req, function (err, fields, files) {
   var filestmp=json.stringify (files, null,);
   if (err) {
    console.log ("parse error:" + err);
   } else {
    console.log ("parse files:" + filestmp);
    var inputfile=files.inputfile [];
    var uploadedpath=inputfile.path;
    var dstpath="./public/files/" + inputfile.originalfilename;
    //Rename to real file name
    fs.rename (uploadedpath, dstpath, function (err) {
     if (err) {
      console.log ("rename error:" + err);
     } else {
      console.log ("rename ok");
   res.writehead (, {"content-type":"text/plain;charset=utf-"});
   res.write ("received upload:\ n \ n");
   res.end (util.inspect ((fields:fields, files:filestmp)));
  • Previous Android development taking notes using the camera to take pictures
  • Next Example of four-level password strength detection function implemented by JS