Skip to content
Branch: master
Find file History

express-fileupload Examples

Basic File Upload

Your node.js code:

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

// default options
app.use(fileUpload());'/upload', function(req, res) {
  if (Object.keys(req.files).length == 0) {
    return res.status(400).send('No files were uploaded.');

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  let sampleFile = req.files.sampleFile;

  // Use the mv() method to place the file somewhere on your server'/somewhere/on/your/server/filename.jpg', function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');

Your HTML file upload form:

    <form ref='uploadForm' 
        <input type="file" name="sampleFile" />
        <input type='submit' value='Upload!' />

Multi-File Upload

express-fileupload supports multiple file uploads at the same time.

Let's say you have three files in your form, each of the inputs with the name my_profile_pic, my_pet, and my_cover_photo:

<input type="file" name="my_profile_pic" />
<input type="file" name="my_pet" />
<input type="file" name="my_cover_photo" />

These uploaded files would be accessible like so:'/upload', function(req, res) {
  // Uploaded files:
You can’t perform that action at this time.