MultipartParser
Summary
Defines a multipart parser to parse multipart requests.
Use MultipartParser to get fields from multipart requests (including files).
Constructor
MultipartParser
Syntax
MultipartParser
(
-
request
-
fileFields
-
[limits]
Summary
Parameters:
-
request
RequestHTTP Request containing a multipart body, it will be altered with parsing properties
-
fileFields
ArrayA list of file field descriptors with:
- {String} name The field name which contains the file
- {String} destinationPath The destination directory where the file will be uploaded
- {Number} [maxCount] The maximum number of files allowed for this field
- {Boolean} [unique] 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 optionalMultipart limits configuration, for more information about available limits see Multer documentation (https://www.npmjs.com/package/multer#limits).
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);
});
Item Index
Methods
Properties
Methods
getField
Syntax
Summary
Gets fields configuration by name.
Parameters:
-
fieldName
StringThe name of the field containing files
Returns:
Object | Null:
The field configuration
getFileName
Syntax
getFileName
(
async
-
originalFileName
-
fieldName
-
callback
Summary
Builds final file name.
It avoids collisions with existing files and sanitizes file name.
parse
Syntax
parse
(
async
-
callback
Summary
Parses multipart content of the request and performs uploads if any.
Parameters:
-
callback
FunctionThe function to call when done
- Error The error if an error occurred, null otherwise
Properties
detectedFilesPaths
Syntax
Summary
Final paths of files detected in multipart body.