2
2
<img alt =" npm formidable package logo " src =" https://raw.githubusercontent.com/node-formidable/formidable/master/logo.png " />
3
3
</p >
4
4
5
- # formidable [ ![ npm version] [ npmv-img ]] [ npmv-url ] [ ![ MIT license] [ license-img ]] [ license-url ] [ ![ Libera Manifesto] [ libera-manifesto-img ]] [ libera-manifesto-url ] [ ![ Twitter ] [ twitter-img ]] [ twitter-url ]
5
+ # formidable [ ![ npm version] [ npmv-img ]] [ npmv-url ] [ ![ MIT license] [ license-img ]] [ license-url ] [ ![ Libera Manifesto] [ libera-manifesto-img ]] [ libera-manifesto-url ]
6
6
7
7
> A Node.js module for parsing form data, especially file uploads.
8
8
9
+ > [ !CAUTION] As of April 2025, old versions like v1 and v2 are still the most
10
+ > used, while they are deperecated for years -- they are also vulnerable to
11
+ > attacks if you are not implementing it properly. ** Please upgrade!** We are
12
+ > here to help, and AI Editors & Agents could help a lot in such codemod-like
13
+ > migrations.
14
+
15
+ > [ !TIP] If you are starting a fresh project, try ` formidable@latest ` (v3) or
16
+ > you can check out the ` formidable-mini ` which is a super minimal version of
17
+ > Formidable (not quite configurable yet, but when it does it could become the
18
+ > basis for ` formidable@v4 ` ), using web standards like FormData API and File
19
+ > API, and you can use it to stream uploads directly to S3 or other such
20
+ > services.
21
+
9
22
[ ![ Code style] [ codestyle-img ]] [ codestyle-url ]
10
- [ ![ codecoverage] [ codecov-img ]] [ codecov-url ]
11
23
[ ![ linux build status] [ linux-build-img ]] [ build-url ]
12
- [ ![ windows build status] [ windows-build-img ]] [ build-url ]
13
24
[ ![ macos build status] [ macos-build-img ]] [ build-url ]
14
25
15
26
If you have any _ how-to_ kind of questions, please read the [ Contributing
16
27
Guide] [ contributing-url ] and [ Code of Conduct] [ code_of_conduct-url ]
17
28
documents.<br /> For bugs reports and feature requests, [ please create an
18
- issue] [ open-issue-url ] or ping [ @ tunnckoCore / @ 3a1FcBx0 ] ( https://twitter.com/3a1FcBx0 )
19
- at Twitter.
29
+ issue] [ open-issue-url ] or ping
30
+ [ @wgw_eth / @wgw_lol ] ( https://twitter.com/wgw_eth ) at Twitter.
20
31
21
32
[ ![ Conventional Commits] [ ccommits-img ]] [ ccommits-url ]
22
33
[ ![ Minimum Required Nodejs] [ nodejs-img ]] [ npmv-url ]
34
+ [ ![ Buy me a Kofi] [ kofi-img ]] [ kofi-url ]
35
+ [ ![ Make A Pull Request] [ prs-welcome-img ]] [ prs-welcome-url ]
36
+ [ ![ Twitter] [ twitter-img ]] [ twitter-url ]
37
+
38
+ <!-- [![Conventional Commits][ccommits-img]][ccommits-url]
39
+ [![Minimum Required Nodejs][nodejs-img]][npmv-url]
23
40
[![Tidelift Subcsription][tidelift-img]][tidelift-url]
24
41
[![Buy me a Kofi][kofi-img]][kofi-url]
25
42
[![Renovate App Status][renovateapp-img]][renovateapp-url]
26
- [ ![ Make A Pull Request] [ prs-welcome-img ]] [ prs-welcome-url ]
43
+ [![Make A Pull Request][prs-welcome-img]][prs-welcome-url] -->
27
44
28
- This project is [ semantically versioned] ( https://semver.org ) and available as
45
+ <!-- This project is [semantically versioned](https://semver.org) and available as
29
46
part of the [Tidelift Subscription][tidelift-url] for professional grade
30
47
assurances, enhanced support and security.
31
48
[Learn more.](https://tidelift.com/subscription/pkg/npm-formidable?utm_source=npm-formidable&utm_medium=referral&utm_campaign=enterprise)
@@ -34,14 +51,16 @@ _The maintainers of `formidable` and thousands of other packages are working
34
51
with Tidelift to deliver commercial support and maintenance for the Open Source
35
52
dependencies you use to build your applications. Save time, reduce risk, and
36
53
improve code health, while paying the maintainers of the exact dependencies you
37
- use._
54
+ use._ -->
38
55
39
56
[ ![ ] [ npm-weekly-img ]] [ npmv-url ] [ ![ ] [ npm-monthly-img ]] [ npmv-url ]
40
57
[ ![ ] [ npm-yearly-img ]] [ npmv-url ] [ ![ ] [ npm-alltime-img ]] [ npmv-url ]
41
58
42
59
## Project Status: Maintained
43
60
44
- _ Check [ VERSION NOTES] ( https://github.com/node-formidable/formidable/blob/master/VERSION_NOTES.md ) for more information on v1, v2, and v3 plans, NPM dist-tags and branches._
61
+ _ Check
62
+ [ VERSION NOTES] ( https://github.com/node-formidable/formidable/blob/master/VERSION_NOTES.md )
63
+ for more information on v1, v2, and v3 plans, NPM dist-tags and branches._
45
64
46
65
This module was initially developed by
47
66
[ ** @felixge ** ] ( https://github.com/felixge ) for
@@ -73,7 +92,9 @@ This project requires `Node.js >= 10.13`. Install it using
73
92
recommend to use Yarn when you think to contribute to this project._
74
93
75
94
This is a low-level package, and if you're using a high-level framework it _ may_
76
- already be included. Check the examples below and the [ examples/] ( https://github.com/node-formidable/formidable/tree/master/examples ) folder.
95
+ already be included. Check the examples below and the
96
+ [ examples/] ( https://github.com/node-formidable/formidable/tree/master/examples )
97
+ folder.
77
98
78
99
``` sh
79
100
# v2
@@ -85,8 +106,8 @@ npm install formidable@v2
85
106
npm install formidable@v3
86
107
```
87
108
88
- _ ** Note:** In near future v3 will be published on the ` latest ` NPM dist-tag. Future not ready releases will continue to be published on ` canary ` dist-tag. _
89
-
109
+ _ ** Note:** In near future v3 will be published on the ` latest ` NPM dist-tag.
110
+ Future not ready releases will continue to be published on ` canary ` dist-tag. _
90
111
91
112
## Examples
92
113
@@ -325,12 +346,13 @@ See it's defaults in [src/Formidable.js DEFAULT_OPTIONS](./src/Formidable.js)
325
346
uploaded file.
326
347
- ` options.maxFileSize ` ** {number}** - default ` 200 * 1024 * 1024 ` (200mb);
327
348
limit the size of uploaded file.
328
- - ` options.maxFields ` ** {number}** - default ` 1000 ` ; limit the number of fields, set 0 for unlimited
349
+ - ` options.maxFields ` ** {number}** - default ` 1000 ` ; limit the number of fields,
350
+ set 0 for unlimited
329
351
- ` options.maxFieldsSize ` ** {number}** - default ` 20 * 1024 * 1024 ` (20mb);
330
352
limit the amount of memory all fields together (except files) can allocate in
331
353
bytes.
332
- - ` options.hashAlgorithm ` ** {string | false}** - default ` false ` ; include checksums calculated
333
- for incoming files, set this to some hash algorithm, see
354
+ - ` options.hashAlgorithm ` ** {string | false}** - default ` false ` ; include
355
+ checksums calculated for incoming files, set this to some hash algorithm, see
334
356
[ crypto.createHash] ( https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options )
335
357
for available algorithms
336
358
- ` options.fileWriteStreamHandler ` ** {function}** - default ` null ` , which by
@@ -354,8 +376,7 @@ See it's defaults in [src/Formidable.js DEFAULT_OPTIONS](./src/Formidable.js)
354
376
- ` options.filter ` ** {function}** - default function that always returns true.
355
377
Use it to filter files before they are uploaded. Must return a boolean.
356
378
357
-
358
- #### ` options.filename ` ** {function}** function (name, ext, part, form) -> string
379
+ #### ` options.filename ` ** {function}** function (name, ext, part, form) -> string
359
380
360
381
_ ** Note:** If this size of combined fields, or size of some file is exceeded, an
361
382
` 'error' ` event is fired._
@@ -370,20 +391,19 @@ form.bytesReceived;
370
391
form .bytesExpected ;
371
392
```
372
393
373
- #### ` options.filter ` ** {function}** function ({name, originalFilename, mimetype}) -> boolean
394
+ #### ` options.filter ` ** {function}** function ({name, originalFilename, mimetype}) -> boolean
374
395
375
- ** Note:** use an outside variable to cancel all uploads upon the first error
396
+ ** Note:** use an outside variable to cancel all uploads upon the first error
376
397
377
398
``` js
378
399
const options = {
379
- filter : function ({name, originalFilename, mimetype}) {
400
+ filter : function ({ name, originalFilename, mimetype }) {
380
401
// keep only images
381
- return mimetype && mimetype .includes (" image" );
382
- }
402
+ return mimetype && mimetype .includes (' image' );
403
+ },
383
404
};
384
405
```
385
406
386
-
387
407
### .parse(request, callback)
388
408
389
409
Parses an incoming Node.js ` request ` containing form data. If ` callback ` is
@@ -405,41 +425,41 @@ multipart stream. Doing so will disable any `'field'` / `'file'` events
405
425
processing which would occur otherwise, making you fully responsible for
406
426
handling the processing.
407
427
408
- About ` uploadDir ` , given the following directory structure
428
+ About ` uploadDir ` , given the following directory structure
429
+
409
430
```
410
431
project-name
411
432
├── src
412
433
│ └── server.js
413
- │
434
+ │
414
435
└── uploads
415
436
└── image.jpg
416
437
```
417
438
418
439
` __dirname ` would be the same directory as the source file itself (src)
419
440
420
-
421
441
``` js
422
- ` ${ __dirname } /../uploads`
442
+ ` ${ __dirname } /../uploads` ;
423
443
```
424
444
425
445
to put files in uploads.
426
446
427
- Omitting ` __dirname ` would make the path relative to the current working directory. This would be the same if server.js is launched from src but not project-name.
428
-
447
+ Omitting ` __dirname ` would make the path relative to the current working
448
+ directory. This would be the same if server.js is launched from src but not
449
+ project-name.
429
450
430
451
` null ` will use default which is ` os.tmpdir() `
431
452
432
- Note: If the directory does not exist, the uploaded files are __ silently discarded__ . To make sure it exists:
453
+ Note: If the directory does not exist, the uploaded files are ** silently
454
+ discarded** . To make sure it exists:
433
455
434
456
``` js
435
- import {createNecessaryDirectoriesSync } from " filesac" ;
436
-
457
+ import { createNecessaryDirectoriesSync } from ' filesac' ;
437
458
438
459
const uploadPath = ` ${ __dirname } /../uploads` ;
439
460
createNecessaryDirectoriesSync (` ${ uploadPath} /x` );
440
461
```
441
462
442
-
443
463
In the example below, we listen on couple of events and direct them to the
444
464
` data ` listener, so you can do whatever you choose there, based on whether its
445
465
before the file been emitted, the header value, the header name, on field, on
@@ -468,30 +488,33 @@ form.once('end', () => {
468
488
});
469
489
470
490
// If you want to customize whatever you want...
471
- form .on (' data' , ({ name, key, value, buffer, start, end, formname, ... more }) => {
472
- if (name === ' partBegin' ) {
473
- }
474
- if (name === ' partData' ) {
475
- }
476
- if (name === ' headerField' ) {
477
- }
478
- if (name === ' headerValue' ) {
479
- }
480
- if (name === ' headerEnd' ) {
481
- }
482
- if (name === ' headersEnd' ) {
483
- }
484
- if (name === ' field' ) {
485
- console .log (' field name:' , key);
486
- console .log (' field value:' , value);
487
- }
488
- if (name === ' file' ) {
489
- console .log (' file:' , formname, value);
490
- }
491
- if (name === ' fileBegin' ) {
492
- console .log (' fileBegin:' , formname, value);
493
- }
494
- });
491
+ form .on (
492
+ ' data' ,
493
+ ({ name, key, value, buffer, start, end, formname, ... more }) => {
494
+ if (name === ' partBegin' ) {
495
+ }
496
+ if (name === ' partData' ) {
497
+ }
498
+ if (name === ' headerField' ) {
499
+ }
500
+ if (name === ' headerValue' ) {
501
+ }
502
+ if (name === ' headerEnd' ) {
503
+ }
504
+ if (name === ' headersEnd' ) {
505
+ }
506
+ if (name === ' field' ) {
507
+ console .log (' field name:' , key);
508
+ console .log (' field value:' , value);
509
+ }
510
+ if (name === ' file' ) {
511
+ console .log (' file:' , formname, value);
512
+ }
513
+ if (name === ' fileBegin' ) {
514
+ console .log (' fileBegin:' , formname, value);
515
+ }
516
+ },
517
+ );
495
518
```
496
519
497
520
### .use(plugin: Plugin)
@@ -603,7 +626,7 @@ export interface File {
603
626
604
627
// The name this file had according to the uploading client.
605
628
file .originalFilename : string | null ;
606
-
629
+
607
630
// calculated based on options provided
608
631
file .newFilename : string | null ;
609
632
@@ -653,12 +676,12 @@ file system.
653
676
654
677
``` js
655
678
form .on (' fileBegin' , (formName , file ) => {
656
- // accessible here
657
- // formName the name in the form (<input name="thisname" type="file">) or http filename for octetstream
658
- // file.originalFilename http filename or null if there was a parsing error
659
- // file.newFilename generated hexoid or what options.filename returned
660
- // file.filepath default pathnme as per options.uploadDir and options.filename
661
- // file.filepath = CUSTOM_PATH // to change the final path
679
+ // accessible here
680
+ // formName the name in the form (<input name="thisname" type="file">) or http filename for octetstream
681
+ // file.originalFilename http filename or null if there was a parsing error
682
+ // file.newFilename generated hexoid or what options.filename returned
683
+ // file.filepath default pathnme as per options.uploadDir and options.filename
684
+ // file.filepath = CUSTOM_PATH // to change the final path
662
685
});
663
686
```
664
687
@@ -669,9 +692,9 @@ Emitted whenever a field / file pair has been received. `file` is an instance of
669
692
670
693
``` js
671
694
form .on (' file' , (formname , file ) => {
672
- // same as fileBegin, except
673
- // it is too late to change file.filepath
674
- // file.hash is available if options.hash was used
695
+ // same as fileBegin, except
696
+ // it is too late to change file.filepath
697
+ // file.hash is available if options.hash was used
675
698
});
676
699
```
677
700
@@ -766,8 +789,10 @@ Thanks goes to these wonderful people
766
789
From a [ Felix blog post] ( https://felixge.de/2013/03/11/the-pull-request-hack/ ) :
767
790
768
791
- [ Sven Lito] ( https://github.com/svnlto ) for fixing bugs and merging patches
769
- - [ egirshov] ( https://github.com/egirshov ) for contributing many improvements to the node-formidable multipart parser
770
- - [ Andrew Kelley] ( https://github.com/superjoe30 ) for also helping with fixing bugs and making improvements
792
+ - [ egirshov] ( https://github.com/egirshov ) for contributing many improvements to
793
+ the node-formidable multipart parser
794
+ - [ Andrew Kelley] ( https://github.com/superjoe30 ) for also helping with fixing
795
+ bugs and making improvements
771
796
- [ Mike Frey] ( https://github.com/mikefrey ) for contributing JSON support
772
797
773
798
## License
@@ -795,8 +820,8 @@ Formidable is licensed under the [MIT License][license-url].
795
820
[ renovateapp-img ] : https://badgen.net/badge/renovate/enabled/green?cache=300
796
821
[ prs-welcome-img ] : https://badgen.net/badge/PRs/welcome/green?cache=300
797
822
[ prs-welcome-url ] : http://makeapullrequest.com
798
- [ twitter-url ] : https://twitter.com/3a1fcBx0
799
- [ twitter-img ] : https://badgen.net/twitter/follow/3a1fcBx0 ?icon=twitter&color=1da1f2&cache=300
823
+ [ twitter-url ] : https://twitter.com/wgw_eth
824
+ [ twitter-img ] : https://badgen.net/badge/ twitter/follow/wgw_eth ?icon=twitter&color=1da1f2&cache=30
800
825
801
826
[ npm-weekly-img ] : https://badgen.net/npm/dw/formidable?icon=npm&cache=300
802
827
[ npm-monthly-img ] : https://badgen.net/npm/dm/formidable?icon=npm&cache=300
@@ -819,8 +844,8 @@ Formidable is licensed under the [MIT License][license-url].
819
844
[ kofi-url ] : https://ko-fi.com/tunnckoCore/commissions
820
845
[ kofi-img ] : https://badgen.net/badge/ko-fi/support/29abe0c2?cache=300&icon=https://rawcdn.githack.com/tunnckoCore/badgen-icons/f8264c6414e0bec449dd86f2241d50a9b89a1203/icons/kofi.svg
821
846
822
- [ linux-build-img ] : https://badgen.net/github/checks/node-formidable/formidable/master/ubuntu?cache=300& label=linux%20build&icon=github
823
- [ macos-build-img ] : https://badgen.net/github/checks/node-formidable/formidable/master/macos?cache=300& label=macos%20build&icon=github
847
+ [ linux-build-img ] : https://badgen.net/github/checks/node-formidable/formidable/master? label=linux%20build&icon=github
848
+ [ macos-build-img ] : https://badgen.net/github/checks/node-formidable/formidable/master? label=macos%20build&icon=github
824
849
[ windows-build-img ] : https://badgen.net/github/checks/node-formidable/formidable/master/windows?cache=300&label=windows%20build&icon=github
825
- [ build-url ] : https://github.com/node-formidable/formidable/actions?query=workflow%3Anodejs
850
+ [ build-url ] : https://github.com/node-formidable/formidable/actions
826
851
<!-- prettier-ignore-end -->
0 commit comments