Class: MultipartParser

multipart/MultipartParser~MultipartParser(request, fileFields, limitsopt)

MultipartParser

Constructor

new MultipartParser(request, fileFields, limitsopt)

Defines a multipart parser to parse multipart requests.

Use MultipartParser to get fields from multipart requests (including files).

Parameters:
Name Type Attributes Description
request Object

HTTP Request containing a multipart body, it will be altered with parsing properties

fileFields Array

A list of file field descriptors with:

Properties
Name Type Attributes Description
name String

The field name which contains the file

destinationPath String

The destination directory where the file will be uploaded

maxCount Number <optional>

The maximum number of files allowed for this field

unique Boolean <optional>

true to generate unique file names for files corresponding to this field, false to generate a unique id only if a file with the same name already exists in the destination folder

limits Object <optional>

Multipart limits configuration, for more information about available limits see Multer documentation.

Source:
Throws:

If request is not as expected

Type
TypeError
Example
// Get multipart parser
var MultipartParser = require('@openveo/api').multipart.MultipartParser;

// Create a request parser expecting several files: files in photos "field" and a file in "videos" field
var parser = new MultipartParser(request, [
  {
    name: 'photos',
    destinationPath: '/tmp/photos',
    maxCount: 2,
    unique: true
  },
  {
    name: 'videos',
    destinationPath: '/tmp/videos',
    maxCount: 1,
    unique: false
  }
], {
  fieldNameSize: 100,
  fieldSize: 1024,
  fields: Infinity,
  fileSize: Infinity,
  files: Infinity,
  parts: Infinity,
  headerPairs: 2000
});

parser.parse(function(error) {
  if (error)
    console.log('Something went wrong when uploading');
  else
    console.log(request.files);
});

Members

(readonly) detectedFilesPaths :Array

Final paths of files detected in multipart body.

Type:
  • Array
Source:

(readonly) fileFields :Array

The list of file field descriptors.

Type:
  • Array
Source:

(readonly) limits :Object

Multipart limits configuration.

Type:
  • Object
Source:

(readonly) request :Object

The HTTP request containing a multipart body.

Type:
  • Object
Source:

Methods

getField(fieldName) → {Object|null}

Gets fields configuration by name.

Parameters:
Name Type Description
fieldName String

The name of the field containing files

Source:
Returns:

The field configuration

Type
Object | null

getFileName(originalFileName, fieldName, callback)

Builds final file name.

It avoids collisions with existing files and sanitizes file name.

Parameters:
Name Type Description
originalFileName String

The original file name

fieldName String

The name of the field containing the file

callback module:multipart/MultipartParser~MultipartParser~getFileNameCallback

The function to call when done

Source:

parse(callback)

Parses multipart content of the request and performs uploads if any.

Parameters:
Name Type Description
callback callback

The function to call when done

Source:

Type Definitions

getFileNameCallback(error, fileName)

Parameters:
Name Type Description
error Error | null

The error if an error occurred, null otherwise

fileName String | Undefined

The computed file name

Source: