You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case you need to handle a text-only multipart form, you can use any of the multer methods (`.single()`, `.array()`, `fields()`). Here is an example using `.array()`:
49
+
In case you need to handle a text-only multipart form, you can use the `.none()` method, example:
50
50
51
51
```javascript
52
+
var multer =require('multer')
52
53
var express =require('express')
54
+
53
55
var app =express()
54
-
var multer =require('multer')
55
56
var upload =multer()
56
57
57
-
app.post('/profile', upload.array(), function (req, res, next) {
58
+
app.post('/profile', upload.none(), function (req, res, next) {
`originalname` | Name of the file on the user's computer |
72
-
`encoding` | Encoding type of the file |
73
-
`mimetype` | Mime type of the file |
74
-
`size` | Size of the file in bytes |
75
-
`destination` | The folder to which the file has been saved | `DiskStorage`
76
-
`filename` | The name of the file within the `destination` | `DiskStorage`
77
-
`path` | The full path to the uploaded file | `DiskStorage`
78
-
`buffer` | A `Buffer` of the entire file | `MemoryStorage`
79
-
80
-
### `multer(opts)`
81
-
82
-
Multer accepts an options object, the most basic of which is the `dest`
83
-
property, which tells Multer where to upload the files. In case you omit the
84
-
options object, the files will be kept in memory and never written to disk.
85
-
86
-
By default, Multer will rename the files so as to avoid naming conflicts. The
87
-
renaming function can be customized according to your needs.
88
-
89
-
The following are the options that can be passed to Multer.
90
-
91
69
Key | Description
92
70
--- | ---
93
-
`dest` or `storage` | Where to store the files
94
-
`fileFilter` | Function to control which files are accepted
95
-
`limits` | Limits of the uploaded data
71
+
`fieldName` | Field name specified in the form
72
+
`originalName` | Name of the file on the user's computer
73
+
`size` | Size of the file in bytes
74
+
`stream` | Stream of file
96
75
97
-
In an average web app, only `dest` might be required, and configured as shown in
98
-
the following example.
76
+
### `multer(opts)`
99
77
100
-
```javascript
101
-
var upload =multer({ dest:'uploads/' })
102
-
```
78
+
Multer accepts an options object, the following are the options that can be
79
+
passed to Multer.
103
80
104
-
If you want more control over your uploads, you'll want to use the `storage`
105
-
option instead of `dest`. Multer ships with storage engines `DiskStorage`
106
-
and `MemoryStorage`; More engines are available from third parties.
81
+
Key | Description
82
+
-------- | -----------
83
+
`limits` | Limits of the uploaded data [(full description)](#limits)
107
84
108
85
#### `.single(fieldname)`
109
86
@@ -146,67 +123,6 @@ Never add multer as a global middleware since a malicious user could upload
146
123
files to a route that you didn't anticipate. Only use this function on routes
147
124
where you are handling the uploaded files.
148
125
149
-
### `storage`
150
-
151
-
#### `DiskStorage`
152
-
153
-
The disk storage engine gives you full control on storing files to disk.
154
-
155
-
```javascript
156
-
var storage =multer.diskStorage({
157
-
destination:function (req, file, cb) {
158
-
cb(null, '/tmp/my-uploads')
159
-
},
160
-
filename:function (req, file, cb) {
161
-
cb(null, file.fieldname+'-'+Date.now())
162
-
}
163
-
})
164
-
165
-
var upload =multer({ storage: storage })
166
-
```
167
-
168
-
There are two options available, `destination` and `filename`. They are both
169
-
functions that determine where the file should be stored.
170
-
171
-
`destination` is used to determine within which folder the uploaded files should
172
-
be stored. This can also be given as a `string` (e.g. `'/tmp/uploads'`). If no
173
-
`destination` is given, the operating system's default directory for temporary
174
-
files is used.
175
-
176
-
**Note:** You are responsible for creating the directory when providing
177
-
`destination` as a function. When passing a string, multer will make sure that
178
-
the directory is created for you.
179
-
180
-
`filename` is used to determine what the file should be named inside the folder.
181
-
If no `filename` is given, each file will be given a random name that doesn't
182
-
include any file extension.
183
-
184
-
**Note:** Multer will not append any file extension for you, your function
185
-
should return a filename complete with an file extension.
186
-
187
-
Each function gets passed both the request (`req`) and some information about
188
-
the file (`file`) to aid with the decision.
189
-
190
-
Note that `req.body` might not have been fully populated yet. It depends on the
191
-
order that the client transmits fields and files to the server.
192
-
193
-
#### `MemoryStorage`
194
-
195
-
The memory storage engine stores the files in memory as `Buffer` objects. It
196
-
doesn't have any options.
197
-
198
-
```javascript
199
-
var storage =multer.memoryStorage()
200
-
var upload =multer({ storage: storage })
201
-
```
202
-
203
-
When using memory storage, the file info will contain a field called
204
-
`buffer` that contains the entire file.
205
-
206
-
**WARNING**: Uploading very large files, or relatively small files in large
207
-
numbers very quickly, can cause your application to run out of memory when
208
-
memory storage is used.
209
-
210
126
### `limits`
211
127
212
128
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).
@@ -225,29 +141,6 @@ Key | Description | Default
225
141
226
142
Specifying the limits can help protect your site against denial of service (DoS) attacks.
227
143
228
-
### `fileFilter`
229
-
230
-
Set this to a function to control which files should be uploaded and which
231
-
should be skipped. The function should look like this:
232
-
233
-
```javascript
234
-
functionfileFilter (req, file, cb) {
235
-
236
-
// The function should call `cb` with a boolean
237
-
// to indicate if the file should be accepted
238
-
239
-
// To reject this file pass `false`, like so:
240
-
cb(null, false)
241
-
242
-
// To accept the file pass `true`, like so:
243
-
cb(null, true)
244
-
245
-
// You can always pass an error if something goes wrong:
246
-
cb(newError('I don\'t have a clue!'))
247
-
248
-
}
249
-
```
250
-
251
144
## Error handling
252
145
253
146
When encountering an error, multer will delegate the error to express. You can
@@ -270,12 +163,3 @@ app.post('/profile', function (req, res) {
270
163
})
271
164
})
272
165
```
273
-
274
-
## Custom storage engine
275
-
276
-
See [the documentation here](/StorageEngine.md) if you want to build your own
0 commit comments