El Mehdi Touimi Benjelloun 015b74845c First commit 5 years ago
..
uploads 015b74845c First commit 5 years ago
README.md 015b74845c First commit 5 years ago
index.html 015b74845c First commit 5 years ago
server.js 015b74845c First commit 5 years ago

README.md

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());

app.post('/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
  sampleFile.mv('/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:

<html>
  <body>
    <form ref='uploadForm' 
      id='uploadForm' 
      action='http://localhost:8000/upload' 
      method='post' 
      encType="multipart/form-data">
        <input type="file" name="sampleFile" />
        <input type='submit' value='Upload!' />
    </form>     
  </body>
</html>

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:

app.post('/upload', function(req, res) {
  // Uploaded files:
  console.log(req.files.my_profile_pic.name);
  console.log(req.files.my_pet.name);
  console.log(req.files.my_cover_photo.name);
});