Multer middleware wrapping for multipart/from-data

WARNING: Make sure that you always handle the files that a user uploads. Never add multer as a global middleware since a malicious user could upload files to a route that you didn't anticipate. Only use this function on routes where you are handling the uploaded files.

Methods

  • Accepts all files that comes over the wire. An array of files will be stored in req.files.

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Accept an array of files, all with the name fieldName. Optionally error out if more than maxCount files are uploaded. The array of files will be stored in req.files

    Parameters

    • fieldName: string

      name of file

    • OptionalmaxCount: number

      maximum number of files

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Accept a mix of files, specified by fields. An object with arrays of files will be stored in req.files.

    Parameters

    • fields: readonly Field[]

      array of entries

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

    [ { name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]
    
  • Accept only text fields. If any file upload is made, error with code "Unexpected field" will be issued.

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Accept a single file with the name fieldName. The single file will be stored in req.file

    Parameters

    • fieldName: string

      name of the file

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Return a new instance of the multipart/form-data middleware that accepts all files that comes over the wire. An array of files will be stored in req.files.

    Parameters

    • Optionalstorage: StorageEngine

      storage engine used for this middleware

    • OptionalallowedMineTypes: string | string[]

      Allowed mime types allowed for this multer instance

    • Optionallimits: {}

      Limits for the middleware

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Return a new instance of the multipart/form-data middleware that accepts an array of files, all with the name fieldName. Optionally error out if more than maxCount files are uploaded. The array of files will be stored in req.files

    Parameters

    • fieldName: string

      name of the file field

    • OptionalmaxCount: number

      maximum number of files

    • Optionalstorage: StorageEngine

      storage engine used for this middleware

    • OptionalallowedMineTypes: string | string[]

      Allowed mime types allowed for this multer instance

    • Optionallimits: {}

      Limits for the middleware

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Return a new instance of the multipart/form-data middleware that accepts a mix of files, specified by fields. An object with arrays of files will be stored in req.files.

    Parameters

    • fields: readonly Field[]

      array of entries

    • Optionalstorage: StorageEngine

      storage engine used for this middleware

    • OptionalallowedMineTypes: string | string[]

      Allowed mime types allowed for this multer instance

    • Optionallimits: {}

      Limits for the middleware

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

    [ { name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]
    
  • Return a new instance of the multipart/form-data middleware

    Parameters

    • Optionalstorage: StorageEngine

      storage engine used for this middleware

    • OptionalallowedMineTypes: string | string[]

      Allowed mime types allowed for this multer instance

    • Optionallimits: {}

      Limits for the middleware

    Returns Multer

  • Return a new instance of the multipart/form-data middleware that accepts only text fields. If any file upload is made, error with code "Unexpected field" will be issued.

    Parameters

    • Optionalstorage: StorageEngine

      storage engine used for this middleware

    • OptionalallowedMineTypes: string | string[]

      Allowed mime types allowed for this multer instance

    • Optionallimits: {}

      Limits for the middleware

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

  • Return a new instance of the multipart/form-data middleware that accepts a single file with the name fieldName. The single file will be stored in req.file

    Parameters

    • fieldName: string

      name of the file field

    • Optionalstorage: StorageEngine

      storage engine used for this middleware

    • OptionalallowedMineTypes: string | string[]

      Allowed mime types allowed for this multer instance

    • Optionallimits: {}

      Limits for the middleware

    Returns RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>