title: Can't upload multiple files using Multer
body: <p>I am unable to select and upload multiple files using Multer in Node.js. </p>
  
  <p>I was able to do a single file upload in a different exercise without any issue, but now I am stuck on multiple uploads function.</p>
  
  <p>What am I mistaking? How can I make it so that the user can select and upload multiple files? Thank for help. </p>
  
  <p><strong>This is my form:</strong></p>
  
  <pre><code>&lt;form class="form" action="/file" method="POST" encType="multipart/form-data"&gt;
  &lt;div class="box"&gt;&lt;/div&gt;
  &lt;div class="form-group"&gt;
    &lt;label&gt;
      &lt;input type='file' name="upload" class="form-control"/&gt;
    &lt;/label&gt;
  &lt;/div&gt;
  &lt;input id="submit" type='submit' value="Submit" class="btn btn-primary"/&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p><strong>My backend code:</strong></p>
  
  <pre><code>
  const express = require('express');
  const bodyParser = require('body-parser');
  const fileUpload = require('express-fileupload');
  const multer = require('multer')
  var fs = require('fs');
  
  // SETUP PACKAGES
  
  const path = require('path');
  const app = express();
  
  const upload = multer({ dest: 'uploads' }) ;
  
  // SETUP MIDDLEWARE
  
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(fileUpload());
  
  app.use(express.static(__dirname));
  
  // DECLARE UPLOAD DIRECTORY
  
  const uploadDirectory = __dirname + path.sep + 'uploads'
  
  // DECLARE PROMISES
  
  const writeFile = (name, body) =&gt; {
      return new Promise((resolve, reject) =&gt; {
              fs.writeFile(uploadDirectory + path.sep + name, body, (err) =&gt; {
                  if (err) {
                          console.error(err)
                  } else {
                          resolve(name)
                  }
              })        
      }).then(readFile);
  }
  
  const readFile = (file) =&gt; {
      return new Promise((resolve, reject) =&gt; {
              fs.readFile(uploadDirectory + path.sep + file, 'utf8', (err, data) =&gt; {
                  if (err) {
                          console.error(err);
                  } else {
                          resolve(data)
                  }
          })
      })
  }
  
  app.get('/',(req,res) =&gt; {
      res.sendFile(path.join(__dirname, 'index.html'));
  });
  
  app.post('/file', upload.array('upload', 5), (req, res) =&gt; {
  
      if(req.files.upload instanceof Array){ 
          for (var i = 0; i &lt; req.files.upload.length; i ++ ){
              let name = req.files.upload[i].name;
              let data = req.files.upload[i].data;
              let write = writeFile(name, data);
  
              console.log(req.files.upload[i].name);
  
              write.then(()=&gt; res.end('fileS uploaded'))
              .catch((e)=&gt; res.status(500).send(e.message));
  
          } 
  
          }else {
  
              // let name = req.files.upload.name;
              // let data = req.files.upload.data;
  
              // writeFile(name, data)
  
              // res.end("file uploaded")
  
      }
      });
  
      app.listen(8080)
  </code></pre>