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

maxFiles options doesn't work #901

Closed
bottom-up-ai opened this issue Nov 4, 2022 · 6 comments
Closed

maxFiles options doesn't work #901

bottom-up-ai opened this issue Nov 4, 2022 · 6 comments
Labels

Comments

@bottom-up-ai
Copy link

Support plan

  • Which support plan is this issue covered by? (Community, Sponsor, Enterprise): Community
  • Currently blocking your project/work? (yes/no): yes
  • Affecting a production system? (yes/no): no

Context

  • Node.js version: v16.17.0
  • Release Line of Formidable (Legacy, Current, Next): Current
  • Formidable exact version: ^2.0.1
  • Environment (node, browser, native, OS): node
  • Used with (popular names of modules): express

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

I try to limit the maxFiles option to 1, but when I upload 3 files, the 3 get uploaded in the corresponding folder.

const form = formidable({
        uploadDir: `${__dirname}/uploads`,
        maxFiles: 1
});
   
form.parse(req, (err, fields, files) => {
        console.log('ERR', err)
        console.log('FIELDS', fields)
        console.log('FILES', files)
});

What was the result you got?

3 files uploaded in folder

What result did you expect?

1 file uploaded in folder

@bottom-up-ai
Copy link
Author

Temporary fix :

let countFileUploaded = 0;

const form = formidable({
        uploadDir: `${__dirname}/uploads`,
        keepExtensions: true,
        // maxFiles: 1,
        filter: () => {
            if (countFileUploaded === 1) return false;

            countFileUploaded += 1;

            return true;
        }
});

@GrosSacASac
Copy link
Contributor

Try again with version 3

@bottom-up-ai
Copy link
Author

bottom-up-ai commented Nov 5, 2022

It breaks my application with version 3, as I'm using CommonJS instead of ESModule.

Will you add support for both CJS & ESM ?

EDIT :

I created another project for testing purposes.

It doesn't seem to work perfectly though.

Scenario 1 :

const form = formidable({
        uploadDir: `./uploads`,
        maxFiles: 1
});

I try to upload 4 files, only 1 gets uploaded : Success.

Scenario 2 :

const form = formidable({
        uploadDir: `./uploads`,
        maxFiles: 2
});

I try to upload 4 files, only 1 gets uploaded : Failure.

Scenario 3 :

const form = formidable({
        uploadDir: `./uploads`,
        maxFiles: 3
});

I try to upload 4 files, only 2 get uploaded : Failure.

Scenario 4 :

const form = formidable({
        uploadDir: `./uploads`,
        maxFiles: 4
});

I try to upload 4 files, only 4 get uploaded : Success.

Scenario 5 :

const form = formidable({
        uploadDir: `./uploads`,
        maxFiles: 5
});

I try to upload 5 files, only 4 get uploaded : Failure.

@tunnckoCore
Copy link
Member

@bottom-up-ai ha, that looks very weird, definitely.

As about CJS support... I don't want to, but unfortunately seems like we should and we will...

@GrosSacASac
Copy link
Contributor

If the number of files uploaded is greater than the max, the whole form is going to error. By the time that happens there maybe a lower number of files written to disk. The files that are ongoing are cancelled.

That is why
Scenario 2 :

const form = formidable({
uploadDir: ./uploads,
maxFiles: 2
});

I try to upload 4 files, only 1 gets uploaded : Failure.
When formidable received the 3rd file the second one still being written to disk is cancelled.

As for scenario 5 I was not able to reproduce it. Please provide a repro case.

@GrosSacASac
Copy link
Contributor

To make formidable not error use the filter option instead. If it does not error it will not cancel ongoing write operations, which means you will have exactly the maximum files uploaded

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

No branches or pull requests

3 participants