Should req.files be an array?
See original GitHub issueI’m really unsure of this myself, but I wanted to get the discussion started. Should req.files
be an array?
It would certainly help #59 since there then would be only one array to check. I also think that the most common case is having one file upload which then would be really easy to find, req.files[0]
.
The downside would be pages that has multiple files with different fieldnames, it would be a bit more complicated to find a specific file.
To mitigate that we could expose a byFieldname(fieldname, multiple)
function. fieldname
would obviously be the filename and multiple
would be a boolean on wether to allow multiple files or not. It would default to false
.
If multiple
is false
and there is more than one file uploaded to that fieldname, I think that it should throw an error.
On top of this we could also expose a byMimetype(mimetype, multiple)
function. That could help with sites that allows uploading any files, and then want to treat music, pictures or movies differently.
Although I feel that we are maybe providing a bit too much with the functions. If req.files
is just a normal array, it’s very easy to do this with both built in functions, and already popular libraries.
examples
// Find all the new photos that should be added to the album
req.files.filter(function (file) {
return (file.fieldname === 'photo')
})
// Check if the user has uploaded a new profile picture
_.find(req.files, function (file) {
return (file.fieldname === 'profile-picture')
})
// Convert all audio files to ogg/vorbis
req.files.filter(function (file) {
return is.is(file.mimetype, ['audio/*'])
})
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:59 (51 by maintainers)
Top GitHub Comments
I had the same problem with TypeScript, but here is a “workaround”, it’s possible to cast this as a array.
req.files as Express.Multer.File[]
I’m having a problem with Typescript type checking regarding with multer.fields(). Typescript can’t check whether I used
req.files.fieldname
or `req.files[fieldname] it always shows an error.When is used
req.files.fieldname
this is the error message from Typescript. " Property ‘Mp3’ does not exist on type ‘{ [fieldname: string]: File[]; } | File[]’. "When is used
req.files[fieldname]
this is the error message from Typescript. " Element implicitly has an ‘any’ type because type ‘{ [fieldname: string]: File[]; } | File[]’ has no index signature. "