multer-orm 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,348 +1,348 @@
1
- # Multer [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Test Coverage][test-image]][test-url] [![OpenSSF Scorecard Badge][ossf-scorecard-badge]][ossf-scorecard-visualizer]
2
-
3
- Multer is a node.js middleware for handling `multipart/form-data`, which is primarily used for uploading files. It is written
4
- on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency.
5
-
6
- **NOTE**: Multer will not process any form which is not multipart (`multipart/form-data`).
7
-
8
- ## Translations
9
-
10
- This README is also available in other languages:
11
-
12
- | | |
13
- | ------------------------------------------------------------------------------ | --------------- |
14
- | [العربية](https://github.com/expressjs/multer/blob/main/doc/README-ar.md) | Arabic |
15
- | [简体中文](https://github.com/expressjs/multer/blob/main/doc/README-zh-cn.md) | Chinese |
16
- | [Français](https://github.com/expressjs/multer/blob/main/doc/README-fr.md) | French |
17
- | [한국어](https://github.com/expressjs/multer/blob/main/doc/README-ko.md) | Korean |
18
- | [Português](https://github.com/expressjs/multer/blob/main/doc/README-pt-br.md) | Portuguese (BR) |
19
- | [Русский язык](https://github.com/expressjs/multer/blob/main/doc/README-ru.md) | Russian |
20
- | [Español](https://github.com/expressjs/multer/blob/main/doc/README-es.md) | Spanish |
21
- | [O'zbek tili](https://github.com/expressjs/multer/blob/main/doc/README-uz.md) | Uzbek |
22
- | [Việt Nam](https://github.com/expressjs/multer/blob/main/doc/README-vi.md) | Vietnamese |
23
-
24
- ## Installation
25
-
26
- ```sh
27
- $ npm install multer
28
- ```
29
-
30
- ## Usage
31
-
32
- Multer adds a `body` object and a `file` or `files` object to the `request` object. The `body` object contains the values of the text fields of the form, the `file` or `files` object contains the files uploaded via the form.
33
-
34
- Basic usage example:
35
-
36
- Don't forget the `enctype="multipart/form-data"` in your form.
37
-
38
- ```html
39
- <form action="/profile" method="post" enctype="multipart/form-data">
40
- <input type="file" name="avatar" />
41
- </form>
42
- ```
43
-
44
- ```javascript
45
- const express = require('express')
46
- const multer = require('multer')
47
- const upload = multer({ dest: 'uploads/' })
48
-
49
- const app = express()
50
-
51
- app.post('/profile', upload.single('avatar'), function (req, res, next) {
52
- // req.file is the `avatar` file
53
- // req.body will hold the text fields, if there were any
54
- })
55
-
56
- app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
57
- // req.files is array of `photos` files
58
- // req.body will contain the text fields, if there were any
59
- })
60
-
61
- const uploadMiddleware = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
62
- app.post('/cool-profile', uploadMiddleware, function (req, res, next) {
63
- // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
64
- //
65
- // e.g.
66
- // req.files['avatar'][0] -> File
67
- // req.files['gallery'] -> Array
68
- //
69
- // req.body will contain the text fields, if there were any
70
- })
71
- ```
72
-
73
- In case you need to handle a text-only multipart form, you should use the `.none()` method:
74
-
75
- ```javascript
76
- const express = require('express')
77
- const app = express()
78
- const multer = require('multer')
79
- const upload = multer()
80
-
81
- app.post('/profile', upload.none(), function (req, res, next) {
82
- // req.body contains the text fields
83
- })
84
- ```
85
-
86
- Here's an example on how multer is used in a HTML form. Take special note of the `enctype="multipart/form-data"` and `name="uploaded_file"` fields:
87
-
88
- ```html
89
- <form action="/stats" enctype="multipart/form-data" method="post">
90
- <div class="form-group">
91
- <input type="file" class="form-control-file" name="uploaded_file">
92
- <input type="text" class="form-control" placeholder="Number of speakers" name="nspeakers">
93
- <input type="submit" value="Get me the stats!" class="btn btn-default">
94
- </div>
95
- </form>
96
- ```
97
-
98
- Then in your javascript file you would add these lines to access both the file and the body. It is important that you use the `name` field value from the form in your upload function. This tells multer which field on the request it should look for the files in. If these fields aren't the same in the HTML form and on your server, your upload will fail:
99
-
100
- ```javascript
101
- const multer = require('multer')
102
- const upload = multer({ dest: './public/data/uploads/' })
103
- app.post('/stats', upload.single('uploaded_file'), function (req, res) {
104
- // req.file is the name of your file in the form above, here 'uploaded_file'
105
- // req.body will hold the text fields, if there were any
106
- console.log(req.file, req.body)
107
- });
108
- ```
109
-
110
-
111
-
112
- ## API
113
-
114
- ### File information
115
-
116
- Each file contains the following information:
117
-
118
- Key | Description | Note
119
- --- | --- | ---
120
- `fieldname` | Field name specified in the form |
121
- `originalname` | Name of the file on the user's computer |
122
- `encoding` | Encoding type of the file |
123
- `mimetype` | Mime type of the file |
124
- `size` | Size of the file in bytes |
125
- `destination` | The folder to which the file has been saved | `DiskStorage`
126
- `filename` | The name of the file within the `destination` | `DiskStorage`
127
- `path` | The full path to the uploaded file | `DiskStorage`
128
- `buffer` | A `Buffer` of the entire file | `MemoryStorage`
129
-
130
- ### `multer(opts)`
131
-
132
- Multer accepts an options object, the most basic of which is the `dest`
133
- property, which tells Multer where to upload the files. In case you omit the
134
- options object, the files will be kept in memory and never written to disk.
135
-
136
- By default, Multer will rename the files so as to avoid naming conflicts. The
137
- renaming function can be customized according to your needs.
138
-
139
- The following are the options that can be passed to Multer.
140
-
141
- Key | Description
142
- --- | ---
143
- `dest` or `storage` | Where to store the files
144
- `fileFilter` | Function to control which files are accepted
145
- `limits` | Limits of the uploaded data
146
- `preservePath` | Keep the full path of files instead of just the base name
147
-
148
- In an average web app, only `dest` might be required, and configured as shown in
149
- the following example.
150
-
151
- ```javascript
152
- const upload = multer({ dest: 'uploads/' })
153
- ```
154
-
155
- If you want more control over your uploads, you'll want to use the `storage`
156
- option instead of `dest`. Multer ships with storage engines `DiskStorage`
157
- and `MemoryStorage`; More engines are available from third parties.
158
-
159
- #### `.single(fieldname)`
160
-
161
- Accept a single file with the name `fieldname`. The single file will be stored
162
- in `req.file`.
163
-
164
- #### `.array(fieldname[, maxCount])`
165
-
166
- Accept an array of files, all with the name `fieldname`. Optionally error out if
167
- more than `maxCount` files are uploaded. The array of files will be stored in
168
- `req.files`.
169
-
170
- #### `.fields(fields)`
171
-
172
- Accept a mix of files, specified by `fields`. An object with arrays of files
173
- will be stored in `req.files`.
174
-
175
- `fields` should be an array of objects with `name` and optionally a `maxCount`.
176
- Example:
177
-
178
- ```javascript
179
- [
180
- { name: 'avatar', maxCount: 1 },
181
- { name: 'gallery', maxCount: 8 }
182
- ]
183
- ```
184
-
185
- #### `.none()`
186
-
187
- Accept only text fields. If any file upload is made, error with code
188
- "LIMIT\_UNEXPECTED\_FILE" will be issued.
189
-
190
- #### `.any()`
191
-
192
- Accepts all files that comes over the wire. An array of files will be stored in
193
- `req.files`.
194
-
195
- **WARNING:** Make sure that you always handle the files that a user uploads.
196
- Never add multer as a global middleware since a malicious user could upload
197
- files to a route that you didn't anticipate. Only use this function on routes
198
- where you are handling the uploaded files.
199
-
200
- ### `storage`
201
-
202
- #### `DiskStorage`
203
-
204
- The disk storage engine gives you full control on storing files to disk.
205
-
206
- ```javascript
207
- const storage = multer.diskStorage({
208
- destination: function (req, file, cb) {
209
- cb(null, '/tmp/my-uploads')
210
- },
211
- filename: function (req, file, cb) {
212
- const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
213
- cb(null, file.fieldname + '-' + uniqueSuffix)
214
- }
215
- })
216
-
217
- const upload = multer({ storage: storage })
218
- ```
219
-
220
- There are two options available, `destination` and `filename`. They are both
221
- functions that determine where the file should be stored.
222
-
223
- `destination` is used to determine within which folder the uploaded files should
224
- be stored. This can also be given as a `string` (e.g. `'/tmp/uploads'`). If no
225
- `destination` is given, the operating system's default directory for temporary
226
- files is used.
227
-
228
- **Note:** You are responsible for creating the directory when providing
229
- `destination` as a function. When passing a string, multer will make sure that
230
- the directory is created for you.
231
-
232
- `filename` is used to determine what the file should be named inside the folder.
233
- If no `filename` is given, each file will be given a random name that doesn't
234
- include any file extension.
235
-
236
- **Note:** Multer will not append any file extension for you, your function
237
- should return a filename complete with a file extension.
238
-
239
- Each function gets passed both the request (`req`) and some information about
240
- the file (`file`) to aid with the decision.
241
-
242
- Note that `req.body` might not have been fully populated yet. It depends on the
243
- order that the client transmits fields and files to the server.
244
-
245
- For understanding the calling convention used in the callback (needing to pass
246
- null as the first param), refer to
247
- [Node.js error handling](https://web.archive.org/web/20220417042018/https://www.joyent.com/node-js/production/design/errors)
248
-
249
- #### `MemoryStorage`
250
-
251
- The memory storage engine stores the files in memory as `Buffer` objects. It
252
- doesn't have any options.
253
-
254
- ```javascript
255
- const storage = multer.memoryStorage()
256
- const upload = multer({ storage: storage })
257
- ```
258
-
259
- When using memory storage, the file info will contain a field called
260
- `buffer` that contains the entire file.
261
-
262
- **WARNING**: Uploading very large files, or relatively small files in large
263
- numbers very quickly, can cause your application to run out of memory when
264
- memory storage is used.
265
-
266
- ### `limits`
267
-
268
- An object specifying the size limits of the following optional properties. Multer passes this object into busboy directly, and the details of the properties can be found on [busboy's page](https://github.com/mscdex/busboy#busboy-methods).
269
-
270
- The following integer values are available:
271
-
272
- Key | Description | Default
273
- --- | --- | ---
274
- `fieldNameSize` | Max field name size | 100 bytes
275
- `fieldSize` | Max field value size (in bytes) | 1MB
276
- `fields` | Max number of non-file fields | Infinity
277
- `fileSize` | For multipart forms, the max file size (in bytes) | Infinity
278
- `files` | For multipart forms, the max number of file fields | Infinity
279
- `parts` | For multipart forms, the max number of parts (fields + files) | Infinity
280
- `headerPairs` | For multipart forms, the max number of header key=>value pairs to parse | 2000
281
-
282
- Specifying the limits can help protect your site against denial of service (DoS) attacks.
283
-
284
- ### `fileFilter`
285
-
286
- Set this to a function to control which files should be uploaded and which
287
- should be skipped. The function should look like this:
288
-
289
- ```javascript
290
- function fileFilter (req, file, cb) {
291
-
292
- // The function should call `cb` with a boolean
293
- // to indicate if the file should be accepted
294
-
295
- // To reject this file pass `false`, like so:
296
- cb(null, false)
297
-
298
- // To accept the file pass `true`, like so:
299
- cb(null, true)
300
-
301
- // You can always pass an error if something goes wrong:
302
- cb(new Error('I don\'t have a clue!'))
303
-
304
- }
305
- ```
306
-
307
- ## Error handling
308
-
309
- When encountering an error, Multer will delegate the error to Express. You can
310
- display a nice error page using [the standard express way](http://expressjs.com/guide/error-handling.html).
311
-
312
- If you want to catch errors specifically from Multer, you can call the
313
- middleware function by yourself. Also, if you want to catch only [the Multer errors](https://github.com/expressjs/multer/blob/main/lib/multer-error.js), you can use the `MulterError` class that is attached to the `multer` object itself (e.g. `err instanceof multer.MulterError`).
314
-
315
- ```javascript
316
- const multer = require('multer')
317
- const upload = multer().single('avatar')
318
-
319
- app.post('/profile', function (req, res) {
320
- upload(req, res, function (err) {
321
- if (err instanceof multer.MulterError) {
322
- // A Multer error occurred when uploading.
323
- } else if (err) {
324
- // An unknown error occurred when uploading.
325
- }
326
-
327
- // Everything went fine.
328
- })
329
- })
330
- ```
331
-
332
- ## Custom storage engine
333
-
334
- For information on how to build your own storage engine, see [Multer Storage Engine](https://github.com/expressjs/multer/blob/main/StorageEngine.md).
335
-
336
- ## License
337
-
338
- [MIT](LICENSE)
339
-
340
- [ci-image]: https://github.com/expressjs/multer/actions/workflows/ci.yml/badge.svg
341
- [ci-url]: https://github.com/expressjs/multer/actions/workflows/ci.yml
342
- [test-url]: https://coveralls.io/r/expressjs/multer?branch=main
343
- [test-image]: https://badgen.net/coveralls/c/github/expressjs/multer/main
344
- [npm-downloads-image]: https://badgen.net/npm/dm/multer
345
- [npm-url]: https://npmjs.org/package/multer
346
- [npm-version-image]: https://badgen.net/npm/v/multer
347
- [ossf-scorecard-badge]: https://api.scorecard.dev/projects/github.com/expressjs/multer/badge
1
+ # Multer [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Test Coverage][test-image]][test-url] [![OpenSSF Scorecard Badge][ossf-scorecard-badge]][ossf-scorecard-visualizer]
2
+
3
+ Multer is a node.js middleware for handling `multipart/form-data`, which is primarily used for uploading files. It is written
4
+ on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency.
5
+
6
+ **NOTE**: Multer will not process any form which is not multipart (`multipart/form-data`).
7
+
8
+ ## Translations
9
+
10
+ This README is also available in other languages:
11
+
12
+ | | |
13
+ | ------------------------------------------------------------------------------ | --------------- |
14
+ | [العربية](https://github.com/expressjs/multer/blob/main/doc/README-ar.md) | Arabic |
15
+ | [简体中文](https://github.com/expressjs/multer/blob/main/doc/README-zh-cn.md) | Chinese |
16
+ | [Français](https://github.com/expressjs/multer/blob/main/doc/README-fr.md) | French |
17
+ | [한국어](https://github.com/expressjs/multer/blob/main/doc/README-ko.md) | Korean |
18
+ | [Português](https://github.com/expressjs/multer/blob/main/doc/README-pt-br.md) | Portuguese (BR) |
19
+ | [Русский язык](https://github.com/expressjs/multer/blob/main/doc/README-ru.md) | Russian |
20
+ | [Español](https://github.com/expressjs/multer/blob/main/doc/README-es.md) | Spanish |
21
+ | [O'zbek tili](https://github.com/expressjs/multer/blob/main/doc/README-uz.md) | Uzbek |
22
+ | [Việt Nam](https://github.com/expressjs/multer/blob/main/doc/README-vi.md) | Vietnamese |
23
+
24
+ ## Installation
25
+
26
+ ```sh
27
+ $ npm install multer
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ Multer adds a `body` object and a `file` or `files` object to the `request` object. The `body` object contains the values of the text fields of the form, the `file` or `files` object contains the files uploaded via the form.
33
+
34
+ Basic usage example:
35
+
36
+ Don't forget the `enctype="multipart/form-data"` in your form.
37
+
38
+ ```html
39
+ <form action="/profile" method="post" enctype="multipart/form-data">
40
+ <input type="file" name="avatar" />
41
+ </form>
42
+ ```
43
+
44
+ ```javascript
45
+ const express = require('express')
46
+ const multer = require('multer')
47
+ const upload = multer({ dest: 'uploads/' })
48
+
49
+ const app = express()
50
+
51
+ app.post('/profile', upload.single('avatar'), function (req, res, next) {
52
+ // req.file is the `avatar` file
53
+ // req.body will hold the text fields, if there were any
54
+ })
55
+
56
+ app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
57
+ // req.files is array of `photos` files
58
+ // req.body will contain the text fields, if there were any
59
+ })
60
+
61
+ const uploadMiddleware = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
62
+ app.post('/cool-profile', uploadMiddleware, function (req, res, next) {
63
+ // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
64
+ //
65
+ // e.g.
66
+ // req.files['avatar'][0] -> File
67
+ // req.files['gallery'] -> Array
68
+ //
69
+ // req.body will contain the text fields, if there were any
70
+ })
71
+ ```
72
+
73
+ In case you need to handle a text-only multipart form, you should use the `.none()` method:
74
+
75
+ ```javascript
76
+ const express = require('express')
77
+ const app = express()
78
+ const multer = require('multer')
79
+ const upload = multer()
80
+
81
+ app.post('/profile', upload.none(), function (req, res, next) {
82
+ // req.body contains the text fields
83
+ })
84
+ ```
85
+
86
+ Here's an example on how multer is used in a HTML form. Take special note of the `enctype="multipart/form-data"` and `name="uploaded_file"` fields:
87
+
88
+ ```html
89
+ <form action="/stats" enctype="multipart/form-data" method="post">
90
+ <div class="form-group">
91
+ <input type="file" class="form-control-file" name="uploaded_file">
92
+ <input type="text" class="form-control" placeholder="Number of speakers" name="nspeakers">
93
+ <input type="submit" value="Get me the stats!" class="btn btn-default">
94
+ </div>
95
+ </form>
96
+ ```
97
+
98
+ Then in your javascript file you would add these lines to access both the file and the body. It is important that you use the `name` field value from the form in your upload function. This tells multer which field on the request it should look for the files in. If these fields aren't the same in the HTML form and on your server, your upload will fail:
99
+
100
+ ```javascript
101
+ const multer = require('multer')
102
+ const upload = multer({ dest: './public/data/uploads/' })
103
+ app.post('/stats', upload.single('uploaded_file'), function (req, res) {
104
+ // req.file is the name of your file in the form above, here 'uploaded_file'
105
+ // req.body will hold the text fields, if there were any
106
+ console.log(req.file, req.body)
107
+ });
108
+ ```
109
+
110
+
111
+
112
+ ## API
113
+
114
+ ### File information
115
+
116
+ Each file contains the following information:
117
+
118
+ Key | Description | Note
119
+ --- | --- | ---
120
+ `fieldname` | Field name specified in the form |
121
+ `originalname` | Name of the file on the user's computer |
122
+ `encoding` | Encoding type of the file |
123
+ `mimetype` | Mime type of the file |
124
+ `size` | Size of the file in bytes |
125
+ `destination` | The folder to which the file has been saved | `DiskStorage`
126
+ `filename` | The name of the file within the `destination` | `DiskStorage`
127
+ `path` | The full path to the uploaded file | `DiskStorage`
128
+ `buffer` | A `Buffer` of the entire file | `MemoryStorage`
129
+
130
+ ### `multer(opts)`
131
+
132
+ Multer accepts an options object, the most basic of which is the `dest`
133
+ property, which tells Multer where to upload the files. In case you omit the
134
+ options object, the files will be kept in memory and never written to disk.
135
+
136
+ By default, Multer will rename the files so as to avoid naming conflicts. The
137
+ renaming function can be customized according to your needs.
138
+
139
+ The following are the options that can be passed to Multer.
140
+
141
+ Key | Description
142
+ --- | ---
143
+ `dest` or `storage` | Where to store the files
144
+ `fileFilter` | Function to control which files are accepted
145
+ `limits` | Limits of the uploaded data
146
+ `preservePath` | Keep the full path of files instead of just the base name
147
+
148
+ In an average web app, only `dest` might be required, and configured as shown in
149
+ the following example.
150
+
151
+ ```javascript
152
+ const upload = multer({ dest: 'uploads/' })
153
+ ```
154
+
155
+ If you want more control over your uploads, you'll want to use the `storage`
156
+ option instead of `dest`. Multer ships with storage engines `DiskStorage`
157
+ and `MemoryStorage`; More engines are available from third parties.
158
+
159
+ #### `.single(fieldname)`
160
+
161
+ Accept a single file with the name `fieldname`. The single file will be stored
162
+ in `req.file`.
163
+
164
+ #### `.array(fieldname[, maxCount])`
165
+
166
+ Accept an array of files, all with the name `fieldname`. Optionally error out if
167
+ more than `maxCount` files are uploaded. The array of files will be stored in
168
+ `req.files`.
169
+
170
+ #### `.fields(fields)`
171
+
172
+ Accept a mix of files, specified by `fields`. An object with arrays of files
173
+ will be stored in `req.files`.
174
+
175
+ `fields` should be an array of objects with `name` and optionally a `maxCount`.
176
+ Example:
177
+
178
+ ```javascript
179
+ [
180
+ { name: 'avatar', maxCount: 1 },
181
+ { name: 'gallery', maxCount: 8 }
182
+ ]
183
+ ```
184
+
185
+ #### `.none()`
186
+
187
+ Accept only text fields. If any file upload is made, error with code
188
+ "LIMIT\_UNEXPECTED\_FILE" will be issued.
189
+
190
+ #### `.any()`
191
+
192
+ Accepts all files that comes over the wire. An array of files will be stored in
193
+ `req.files`.
194
+
195
+ **WARNING:** Make sure that you always handle the files that a user uploads.
196
+ Never add multer as a global middleware since a malicious user could upload
197
+ files to a route that you didn't anticipate. Only use this function on routes
198
+ where you are handling the uploaded files.
199
+
200
+ ### `storage`
201
+
202
+ #### `DiskStorage`
203
+
204
+ The disk storage engine gives you full control on storing files to disk.
205
+
206
+ ```javascript
207
+ const storage = multer.diskStorage({
208
+ destination: function (req, file, cb) {
209
+ cb(null, '/tmp/my-uploads')
210
+ },
211
+ filename: function (req, file, cb) {
212
+ const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
213
+ cb(null, file.fieldname + '-' + uniqueSuffix)
214
+ }
215
+ })
216
+
217
+ const upload = multer({ storage: storage })
218
+ ```
219
+
220
+ There are two options available, `destination` and `filename`. They are both
221
+ functions that determine where the file should be stored.
222
+
223
+ `destination` is used to determine within which folder the uploaded files should
224
+ be stored. This can also be given as a `string` (e.g. `'/tmp/uploads'`). If no
225
+ `destination` is given, the operating system's default directory for temporary
226
+ files is used.
227
+
228
+ **Note:** You are responsible for creating the directory when providing
229
+ `destination` as a function. When passing a string, multer will make sure that
230
+ the directory is created for you.
231
+
232
+ `filename` is used to determine what the file should be named inside the folder.
233
+ If no `filename` is given, each file will be given a random name that doesn't
234
+ include any file extension.
235
+
236
+ **Note:** Multer will not append any file extension for you, your function
237
+ should return a filename complete with a file extension.
238
+
239
+ Each function gets passed both the request (`req`) and some information about
240
+ the file (`file`) to aid with the decision.
241
+
242
+ Note that `req.body` might not have been fully populated yet. It depends on the
243
+ order that the client transmits fields and files to the server.
244
+
245
+ For understanding the calling convention used in the callback (needing to pass
246
+ null as the first param), refer to
247
+ [Node.js error handling](https://web.archive.org/web/20220417042018/https://www.joyent.com/node-js/production/design/errors)
248
+
249
+ #### `MemoryStorage`
250
+
251
+ The memory storage engine stores the files in memory as `Buffer` objects. It
252
+ doesn't have any options.
253
+
254
+ ```javascript
255
+ const storage = multer.memoryStorage()
256
+ const upload = multer({ storage: storage })
257
+ ```
258
+
259
+ When using memory storage, the file info will contain a field called
260
+ `buffer` that contains the entire file.
261
+
262
+ **WARNING**: Uploading very large files, or relatively small files in large
263
+ numbers very quickly, can cause your application to run out of memory when
264
+ memory storage is used.
265
+
266
+ ### `limits`
267
+
268
+ An object specifying the size limits of the following optional properties. Multer passes this object into busboy directly, and the details of the properties can be found on [busboy's page](https://github.com/mscdex/busboy#busboy-methods).
269
+
270
+ The following integer values are available:
271
+
272
+ Key | Description | Default
273
+ --- | --- | ---
274
+ `fieldNameSize` | Max field name size | 100 bytes
275
+ `fieldSize` | Max field value size (in bytes) | 1MB
276
+ `fields` | Max number of non-file fields | Infinity
277
+ `fileSize` | For multipart forms, the max file size (in bytes) | Infinity
278
+ `files` | For multipart forms, the max number of file fields | Infinity
279
+ `parts` | For multipart forms, the max number of parts (fields + files) | Infinity
280
+ `headerPairs` | For multipart forms, the max number of header key=>value pairs to parse | 2000
281
+
282
+ Specifying the limits can help protect your site against denial of service (DoS) attacks.
283
+
284
+ ### `fileFilter`
285
+
286
+ Set this to a function to control which files should be uploaded and which
287
+ should be skipped. The function should look like this:
288
+
289
+ ```javascript
290
+ function fileFilter (req, file, cb) {
291
+
292
+ // The function should call `cb` with a boolean
293
+ // to indicate if the file should be accepted
294
+
295
+ // To reject this file pass `false`, like so:
296
+ cb(null, false)
297
+
298
+ // To accept the file pass `true`, like so:
299
+ cb(null, true)
300
+
301
+ // You can always pass an error if something goes wrong:
302
+ cb(new Error('I don\'t have a clue!'))
303
+
304
+ }
305
+ ```
306
+
307
+ ## Error handling
308
+
309
+ When encountering an error, Multer will delegate the error to Express. You can
310
+ display a nice error page using [the standard express way](http://expressjs.com/guide/error-handling.html).
311
+
312
+ If you want to catch errors specifically from Multer, you can call the
313
+ middleware function by yourself. Also, if you want to catch only [the Multer errors](https://github.com/expressjs/multer/blob/main/lib/multer-error.js), you can use the `MulterError` class that is attached to the `multer` object itself (e.g. `err instanceof multer.MulterError`).
314
+
315
+ ```javascript
316
+ const multer = require('multer')
317
+ const upload = multer().single('avatar')
318
+
319
+ app.post('/profile', function (req, res) {
320
+ upload(req, res, function (err) {
321
+ if (err instanceof multer.MulterError) {
322
+ // A Multer error occurred when uploading.
323
+ } else if (err) {
324
+ // An unknown error occurred when uploading.
325
+ }
326
+
327
+ // Everything went fine.
328
+ })
329
+ })
330
+ ```
331
+
332
+ ## Custom storage engine
333
+
334
+ For information on how to build your own storage engine, see [Multer Storage Engine](https://github.com/expressjs/multer/blob/main/StorageEngine.md).
335
+
336
+ ## License
337
+
338
+ [MIT](LICENSE)
339
+
340
+ [ci-image]: https://github.com/expressjs/multer/actions/workflows/ci.yml/badge.svg
341
+ [ci-url]: https://github.com/expressjs/multer/actions/workflows/ci.yml
342
+ [test-url]: https://coveralls.io/r/expressjs/multer?branch=main
343
+ [test-image]: https://badgen.net/coveralls/c/github/expressjs/multer/main
344
+ [npm-downloads-image]: https://badgen.net/npm/dm/multer
345
+ [npm-url]: https://npmjs.org/package/multer
346
+ [npm-version-image]: https://badgen.net/npm/v/multer
347
+ [ossf-scorecard-badge]: https://api.scorecard.dev/projects/github.com/expressjs/multer/badge
348
348
  [ossf-scorecard-visualizer]: https://ossf.github.io/scorecard-visualizer/#/projects/github.com/expressjs/multer