Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return array of File Objects #400

Closed
blueelvis opened this issue Nov 3, 2016 · 7 comments
Closed

Return array of File Objects #400

blueelvis opened this issue Nov 3, 2016 · 7 comments

Comments

@blueelvis
Copy link

blueelvis commented Nov 3, 2016

Currently, formidable does not return an array of File Objects in the JSON format if only a single file upload takes place. This makes breaks the logic of counting the number of files.

Modified code which corrects this -

lib\incoming_form.js

if (cb) {
    var fields = {}, files = {};
    this
      .on('field', function(name, value) {
        fields[name] = value;
      })
      .on('file', function(name, file) {
        if (this.multiples) {
          if (files[name]) {
            if (!Array.isArray(files[name])) {
              files[name] = [files[name]];
            }
            files[name].push(file);
          } else {
            files[name] = [file];
          }
        } else {
          files[name] = [file];
        }
      })
      .on('error', function(err) {
        cb(err, fields, files);
      })
      .on('end', function() {
        cb(null, fields, files);
      });
  }
@tunnckoCore tunnckoCore added bug and removed patch labels Jan 16, 2017
@tunnckoCore
Copy link
Member

tunnckoCore commented Jan 16, 2017

Thanks! I always thought that is not so comfort too

@tunnckoCore
Copy link
Member

Maybe #315 is fix.

@DylanPiercey
Copy link

Fields can also have multiples, is that a consideration here?

@blueelvis
Copy link
Author

@tunnckoCore - Can I submit a PR?

@kornelski
Copy link
Contributor

Hi! This is a common request, so it's definitely good to add it.

However, please be careful with the implementation.

It's cumbersome and potentially dangerous to have a field that can be an object or an array. It requires the application to check every time, and the application may be tricked into mishandling files if an attacker submits 2 files when the app expects only 1.

So don't reuse any existing fields. Add a new one, which is array-only.

@xarguments xarguments added feature and removed bug labels Jun 24, 2018
@tunnckoCore
Copy link
Member

Just merged #380. We need to add this.multiples check to it too.

@GrosSacASac
Copy link
Contributor

GrosSacASac commented Apr 27, 2021

npm i node-formidable/formidable#3.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants