node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.04k stars 683 forks source link

Properties ending with `[]` are not turned into an array, even with the `multiples: true` option #633

Closed skerit closed 3 years ago

skerit commented 4 years ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

Parsing formdata with multiples: true does not actually result in an array of values.

Here's the formdata (minus the file):

-----------------------------40560332322476987903819948371
Content-Disposition: form-data; name="number"

2019-482
-----------------------------40560332322476987903819948371
Content-Disposition: form-data; name="followed_workshop"

none_ext
-----------------------------40560332322476987903819948371
Content-Disposition: form-data; name="categories[]"

architecture
-----------------------------40560332322476987903819948371
Content-Disposition: form-data; name="categories[]"

product

And here's the code that's processing it:

let form = new formidable.IncomingForm({multiples: true});

form.hash = 'md5';

form.parse(req, function parsedMultipart(err, form_fields, form_files) {
    console.log(form_fields);
});

What was the result you got?

The resulting form_fields object looks like this:

{
    "categories[]": "product",
    "followed_workshop": "none_ext",
    "number": "2019-482"
}

What result did you expect?

Instead of a categories[] property, I expected a categories property with an array of all the submitted values.

auto-comment[bot] commented 4 years ago

Thank you for raising this issue! We will try and get back to you as soon as possible. Please make sure you format it properly, followed our code of conduct and have given us as much context as possible. /cc @tunnckoCore @GrosSacASac

tunnckoCore commented 4 years ago

Yes. v1.x is known with a lot of this bug. The canary tag handles them, see the changelog - a ton is closed, fixed and merged. That's the recommended path anyway, in the same time helps for polishing the v2 before release.

Try and report back please :)

IonelLupu commented 3 years ago

Hey. @tunnckoCore . Thanks for letting us know. Can't wait for v2. Keep up the good work.

IonelLupu commented 3 years ago

@tunnckoCore I just tried using formidable@canary. Sending files using multipart/form-data doesn't turn properties correctly ending with [] or ending with [0] [1] etc in an array. It keeps the square brackets 😢

The following form data: image

is translated to this: image

nawawishkid-old commented 3 years ago

I've just tried using formidable@canary and it works!

IonelLupu commented 3 years ago

@nawawishkid-old I just tried formidable@canary as well, right now, and I get the same thing I said in the previous comment:

form.parse(incomingMessage, (error, content, formidableFiles) => {
  // formidableFiles is an object with the keys containing `[]` and the end for multiple media files, even if the value is an array
})

As show in the image below image

I think the media[] key shouldn't have the [] string at the end

GrosSacASac commented 3 years ago

@IonelLupu Can you show the code you use to submit the form ?

IonelLupu commented 3 years ago

@GrosSacASac I retested the code and it seems it's now working as expected. I don't know what was the issue before. I guess we can close this now

ev3nst commented 3 years ago

Im using canary tag when installing and this problem still exists for me.

Screen Shot 2021-03-22 at 15 01 54

Using as:


import { IncomingForm } from 'formidable';
....
const form = new IncomingForm({
    // @ts-ignore Formidable@canary
    maxFields: 50,
    multiples: true,
});

form.parse(req, function (err, fields, files) {
    if (err) {
        return next(err);
    }

    console.log(files)
GrosSacASac commented 3 years ago

I think it doesn't matter if the form name has []. What counts is if the client sends multiple files or multiples inputs with the same name are sent.

datalogics-charlesm commented 3 years ago

Im using canary tag when installing and this problem still exists for me.

Screen Shot 2021-03-22 at 15 01 54

Using as:

import { IncomingForm } from 'formidable';
....
const form = new IncomingForm({
    // @ts-ignore Formidable@canary
    maxFields: 50,
    multiples: true,
});

form.parse(req, function (err, fields, files) {
    if (err) {
        return next(err);
    }

    console.log(files)

You don't need the [] for files, only for fields, just send multiple files with the same key and you should get an array.