diff --git a/README.md b/README.md index 7f5d0807..2b885785 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ Key | Description `dest` or `storage` | Where to store the files `fileFilter` | Function to control which files are accepted `limits` | Limits of the uploaded data +`parseJsonFields` | Parse fields that have the content-type `application/json` `preservePath` | Keep the full path of files instead of just the base name In an average web app, only `dest` might be required, and configured as shown in @@ -299,6 +300,13 @@ function fileFilter (req, file, cb) { } ``` +### `parseJsonFields` + +Fields may also have a `Content-Type` header. If you set `parseJsonFields` to +`true` these fields will be parsed using `JSON.parse()` instead of handled as +plain text strings. This way you don't need to unroll complex JSON structures +that are transmitted alongside uploaded files as url-encoded fields. + ## Error handling When encountering an error, Multer will delegate the error to Express. You can diff --git a/index.js b/index.js index d5b67eba..5fa7fe4c 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,7 @@ function Multer (options) { this.limits = options.limits this.preservePath = options.preservePath this.fileFilter = options.fileFilter || allowAll + this.parseJsonFields = !!options.parseJsonFields } Multer.prototype._makeMiddleware = function (fields, fileStrategy) { @@ -49,7 +50,8 @@ Multer.prototype._makeMiddleware = function (fields, fileStrategy) { preservePath: this.preservePath, storage: this.storage, fileFilter: wrappedFileFilter, - fileStrategy: fileStrategy + fileStrategy: fileStrategy, + parseJsonFields: this.parseJsonFields } } @@ -79,7 +81,8 @@ Multer.prototype.any = function () { preservePath: this.preservePath, storage: this.storage, fileFilter: this.fileFilter, - fileStrategy: 'ARRAY' + fileStrategy: 'ARRAY', + parseJsonFields: this.parseJsonFields } } diff --git a/lib/make-middleware.js b/lib/make-middleware.js index 6627cf4c..477597d0 100644 --- a/lib/make-middleware.js +++ b/lib/make-middleware.js @@ -19,6 +19,7 @@ function makeMiddleware (setup) { var fileFilter = options.fileFilter var fileStrategy = options.fileStrategy var preservePath = options.preservePath + var parseJsonFields = options.parseJsonFields req.body = Object.create(null) @@ -72,7 +73,7 @@ function makeMiddleware (setup) { } // handle text field data - busboy.on('field', function (fieldname, value, { nameTruncated, valueTruncated }) { + busboy.on('field', function (fieldname, value, { nameTruncated, valueTruncated, mimeType }) { if (fieldname == null) return abortWithCode('MISSING_FIELD_NAME') if (nameTruncated) return abortWithCode('LIMIT_FIELD_KEY') if (valueTruncated) return abortWithCode('LIMIT_FIELD_VALUE', fieldname) @@ -82,6 +83,14 @@ function makeMiddleware (setup) { if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY') } + if (parseJsonFields && mimeType === 'application/json') { + try { + value = JSON.parse(value) + } catch (error) { + return abortWithError(error) + } + } + appendField(req.body, fieldname, value) })