Bird
Raised Fist0
Expressframework~5 mins

Multiple file uploads in Express - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What middleware is commonly used in Express to handle multiple file uploads?
Multer is the popular middleware used in Express to handle multiple file uploads easily.
Click to reveal answer
beginner
How do you specify multiple files upload field in Multer?
Use the upload.array('fieldname', maxCount) method where 'fieldname' is the name of the input field and maxCount is the max number of files allowed.
Click to reveal answer
beginner
What property of the request object holds the array of uploaded files when using upload.array()?
The req.files property contains an array of uploaded file objects when using upload.array().
Click to reveal answer
intermediate
Why is it important to set limits on file size and number of files in multiple uploads?
Setting limits prevents server overload, protects from denial of service attacks, and controls storage usage.
Click to reveal answer
intermediate
How can you handle errors during multiple file uploads in Express with Multer?
You can use error-handling middleware or check for errors in the route callback to respond properly if upload fails or files exceed limits.
Click to reveal answer
Which Multer method is used to upload multiple files from a single input field?
Aupload.array()
Bupload.single()
Cupload.fields()
Dupload.none()
Where are the uploaded files stored temporarily by Multer before you move or process them?
AIn <code>req.body</code>
BIn <code>req.file</code>
CIn <code>req.files</code> array
DIn <code>req.params</code>
What is a good practice to avoid server overload when allowing multiple file uploads?
ADisable file uploads
BSet limits on file size and number of files
CAllow unlimited files and sizes
DOnly accept text files
Which property contains the original name of the uploaded file in Multer's file object?
Aoriginalname
Bfilename
Cpath
Dsize
How do you handle errors from Multer middleware in Express routes?
ARestart the server
BUse a try-catch block around the upload middleware
CIgnore errors and continue
DUse error-handling middleware or check error in callback
Explain how to set up multiple file uploads in an Express app using Multer.
Think about middleware setup and how files are accessed in the request.
You got /5 concepts.
    Describe why setting limits on file size and number of files is important in multiple file uploads.
    Consider server safety and resource management.
    You got /4 concepts.

      Practice

      (1/5)
      1. In Express, which method from multer is used to handle multiple file uploads at once?
      easy
      A. upload.array()
      B. upload.single()
      C. upload.fields()
      D. upload.none()

      Solution

      1. Step 1: Understand multer upload methods

        Multer provides different methods: single() for one file, array() for multiple files, and fields() for multiple fields.
      2. Step 2: Identify method for multiple files

        To upload multiple files under the same field name, upload.array() is used.
      3. Final Answer:

        upload.array() -> Option A
      4. Quick Check:

        Multiple files = upload.array() [OK]
      Hint: Multiple files use upload.array() method in multer [OK]
      Common Mistakes:
      • Using upload.single() for multiple files
      • Confusing upload.fields() with upload.array()
      • Not specifying max count in upload.array()
      2. Which of the following is the correct syntax to accept up to 5 files named 'photos' using multer in Express?
      easy
      A. upload.array('photos', 5)
      B. upload.single('photos', 5)
      C. upload.fields([{ name: 'photos', maxCount: 5 }])
      D. upload.none('photos', 5)

      Solution

      1. Step 1: Recall multer syntax for multiple files

        The method upload.array(fieldname, maxCount) accepts multiple files with the same field name and limits the count.
      2. Step 2: Match syntax to question

        To accept up to 5 files named 'photos', upload.array('photos', 5) is correct.
      3. Final Answer:

        upload.array('photos', 5) -> Option A
      4. Quick Check:

        Max 5 files = upload.array('photos', 5) [OK]
      Hint: Use upload.array('fieldname', maxCount) for multiple files [OK]
      Common Mistakes:
      • Using upload.single() with max count
      • Confusing upload.fields() syntax
      • Using upload.none() incorrectly
      3. Given this Express route using multer:
      app.post('/upload', upload.array('docs', 3), (req, res) => {
        res.send(req.files.length);
      });

      What will be the response if a user uploads 2 files named 'docs'?
      medium
      A. 3
      B. Error
      C. 1
      D. 2

      Solution

      1. Step 1: Understand upload.array behavior

        The middleware upload.array('docs', 3) allows up to 3 files. If user uploads 2 files, both are accepted.
      2. Step 2: Check response code

        The handler sends req.files.length, which is the number of uploaded files, here 2.
      3. Final Answer:

        2 -> Option D
      4. Quick Check:

        Uploaded files count = 2 [OK]
      Hint: req.files.length shows how many files were uploaded [OK]
      Common Mistakes:
      • Assuming maxCount is always returned
      • Confusing req.file with req.files
      • Expecting error if fewer than maxCount files uploaded
      4. What is wrong with this Express route for multiple file uploads?
      app.post('/upload', upload.array('files'), (req, res) => {
        console.log(req.file);
        res.send('Uploaded');
      });
      medium
      A. upload.array() cannot be used for multiple files
      B. upload.array() requires a maxCount parameter
      C. Should use req.files instead of req.file to access multiple files
      D. res.send() must send JSON, not string

      Solution

      1. Step 1: Check multer usage for multiple files

        When using upload.array(), uploaded files are stored in req.files (an array), not req.file.
      2. Step 2: Identify error in code

        The code logs req.file, which will be undefined or incorrect for multiple files.
      3. Final Answer:

        Should use req.files instead of req.file to access multiple files -> Option C
      4. Quick Check:

        Multiple files = req.files [OK]
      Hint: Use req.files (plural) for multiple files, not req.file [OK]
      Common Mistakes:
      • Forgetting req.files vs req.file difference
      • Assuming maxCount is mandatory
      • Thinking upload.array() is invalid
      5. You want to accept multiple files from two different fields: 'images' (max 3 files) and 'documents' (max 2 files). Which multer setup correctly handles this?
      hard
      A. upload.array('images', 3), upload.array('documents', 2)
      B. upload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }])
      C. upload.single('images'), upload.single('documents')
      D. upload.none()

      Solution

      1. Step 1: Understand multer methods for multiple fields

        To handle multiple fields with multiple files each, use upload.fields() with an array of objects specifying field names and max counts.
      2. Step 2: Analyze options

        upload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }]) correctly uses upload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }]). upload.array('images', 3), upload.array('documents', 2) is invalid syntax as you cannot chain two upload.array() calls in one route.
      3. Final Answer:

        upload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }]) -> Option B
      4. Quick Check:

        Multiple fields = upload.fields() [OK]
      Hint: Use upload.fields() for multiple fields with multiple files [OK]
      Common Mistakes:
      • Trying to chain multiple upload.array() calls
      • Using upload.single() for multiple files
      • Using upload.none() which disables file uploads