From ec6270d67fd63da169f223ac4d40897c6a3b03b9 Mon Sep 17 00:00:00 2001 From: Felipe Lavratti Date: Tue, 7 Feb 2017 11:46:52 -0200 Subject: [PATCH] Fix input fields with multiple enabled: - req.files[fieldname] is an Object when no multiple files are sent in the same field. - req.files[fieldname] is an Array of Objects when multiple files are sent for fieldname. --- lib/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index f4eafdb..3d6bfbd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -50,7 +50,7 @@ module.exports = function(options) { console.log('filename yo', filename); } - return req.files[fieldname] = { + var newFile = { name: filename, data: buf, encoding: encoding, @@ -67,6 +67,15 @@ module.exports = function(options) { }); } }; + + if (!req.files.hasOwnProperty(fieldname)) { + req.files[fieldname] = newFile; + } else { + if (req.files[fieldname] instanceof Array) + req.files[fieldname].push(newFile); + else + req.files[fieldname] = [req.files[fieldname], newFile]; + } }); });