Skip to content

Commit e1df247

Browse files
committed
Merge remote-tracking branch 'upstream/lts'
# Conflicts: # lib/make-middleware.js
2 parents 7a2928d + ddb65bd commit e1df247

27 files changed

+1267
-336
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## 1.4.5-lts.1
7+
8+
- No changes
9+
## 1.4.4-lts.1
10+
11+
- Bugfix: Bump busboy to fix CVE-2022-24434 (#1097)
12+
- Breaking: Require Node.js 6.0.0 or later (#1097)
13+
14+
## 1.4.4 - 2021-12-07
15+
16+
- Bugfix: Handle missing field names (#913)
17+
- Docs: Add Vietnamese translation (#803)
18+
- Docs: Improve Spanish translation (#948)
19+
20+
## 1.4.3 - 2021-08-09
21+
22+
- Bugfix: Avoid deprecated pseudoRandomBytes function (#774)
23+
- Docs: Add Português Brazil translation for README (#758)
24+
- Docs: Clarify the callback calling convention (#775)
25+
- Docs: Add example on how to link to html multipart form (#580)
26+
- Docs: Add Spanish translation for README (#838)
27+
- Docs: Add Math.random() to storage filename example (#841)
28+
- Docs: Fix mistakes in russian doc (#869)
29+
- Docs: Improve Português Brazil translation (#877)
30+
- Docs: Update var to const in all Readmes (#1024)
31+
- Internal: Bump mkdirp version (#862)
32+
- Internal: Bump Standard version (#878)
33+
34+
## 1.4.2 - 2019-07-16
35+
36+
- Docs: Add Russian translation for README (#662)
37+
- Docs: Patch zh-CN README base on newest README (#670)
38+
- Docs: Fix broken link in Readme (#679)
39+
- Docs: Fix broken link in Chinese Readme (#730)
40+
- Docs: Fix typo in Russian README (#738)
41+
- Docs: Add unit for fieldSize in busboy limit params (#734)
42+
- Internal: Make unit tests comaptible with Node.js 13.x (#752)
43+
644
## 1.4.1 - 2018-10-11
745

846
- Bugfix: Make sure that req.file.buffer always is a Buffer

README.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency.
99

1010
This README is also available in other languages:
1111

12+
- [Español](https://github.com/expressjs/multer/blob/master/doc/README-es.md) (Spanish)
1213
- [简体中文](https://github.com/expressjs/multer/blob/master/doc/README-zh-cn.md) (Chinese)
1314
- [한국어](https://github.com/expressjs/multer/blob/master/doc/README-ko.md) (Korean)
1415
- [Русский язык](https://github.com/expressjs/multer/blob/master/doc/README-ru.md) (Russian)
16+
- [Việt Nam](https://github.com/expressjs/multer/blob/master/doc/README-vi.md) (Vietnam)
17+
- [Português](https://github.com/expressjs/multer/blob/master/doc/README-pt-br.md) (Portuguese Brazil)
1518

1619
## Installation
1720

@@ -34,11 +37,11 @@ Don't forget the `enctype="multipart/form-data"` in your form.
3437
```
3538

3639
```javascript
37-
var express = require('express')
38-
var multer = require('multer')
39-
var upload = multer({ dest: 'uploads/' })
40+
const express = require('express')
41+
const multer = require('multer')
42+
const upload = multer({ dest: 'uploads/' })
4043

41-
var app = express()
44+
const app = express()
4245

4346
app.post('/profile', upload.single('avatar'), function (req, res, next) {
4447
// req.file is the `avatar` file
@@ -50,7 +53,7 @@ app.post('/photos/upload', upload.array('photos', 12), function (req, res, next)
5053
// req.body will contain the text fields, if there were any
5154
})
5255

53-
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
56+
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
5457
app.post('/cool-profile', cpUpload, function (req, res, next) {
5558
// req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
5659
//
@@ -65,16 +68,42 @@ app.post('/cool-profile', cpUpload, function (req, res, next) {
6568
In case you need to handle a text-only multipart form, you should use the `.none()` method:
6669

6770
```javascript
68-
var express = require('express')
69-
var app = express()
70-
var multer = require('multer')
71-
var upload = multer()
71+
const express = require('express')
72+
const app = express()
73+
const multer = require('multer')
74+
const upload = multer()
7275

7376
app.post('/profile', upload.none(), function (req, res, next) {
7477
// req.body contains the text fields
7578
})
7679
```
7780

81+
Here's an example on how multer is used an HTML form. Take special note of the `enctype="multipart/form-data"` and `name="uploaded_file"` fields:
82+
83+
```html
84+
<form action="/stats" enctype="multipart/form-data" method="post">
85+
<div class="form-group">
86+
<input type="file" class="form-control-file" name="uploaded_file">
87+
<input type="text" class="form-control" placeholder="Number of speakers" name="nspeakers">
88+
<input type="submit" value="Get me the stats!" class="btn btn-default">
89+
</div>
90+
</form>
91+
```
92+
93+
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:
94+
95+
```javascript
96+
const multer = require('multer')
97+
const upload = multer({ dest: './public/data/uploads/' })
98+
app.post('/stats', upload.single('uploaded_file'), function (req, res) {
99+
// req.file is the name of your file in the form above, here 'uploaded_file'
100+
// req.body will hold the text fields, if there were any
101+
console.log(req.file, req.body)
102+
});
103+
```
104+
105+
106+
78107
## API
79108

80109
### File information
@@ -115,7 +144,7 @@ In an average web app, only `dest` might be required, and configured as shown in
115144
the following example.
116145

117146
```javascript
118-
var upload = multer({ dest: 'uploads/' })
147+
const upload = multer({ dest: 'uploads/' })
119148
```
120149

121150
If you want more control over your uploads, you'll want to use the `storage`
@@ -170,16 +199,17 @@ where you are handling the uploaded files.
170199
The disk storage engine gives you full control on storing files to disk.
171200

172201
```javascript
173-
var storage = multer.diskStorage({
202+
const storage = multer.diskStorage({
174203
destination: function (req, file, cb) {
175204
cb(null, '/tmp/my-uploads')
176205
},
177206
filename: function (req, file, cb) {
178-
cb(null, file.fieldname + '-' + Date.now())
207+
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
208+
cb(null, file.fieldname + '-' + uniqueSuffix)
179209
}
180210
})
181211

182-
var upload = multer({ storage: storage })
212+
const upload = multer({ storage: storage })
183213
```
184214

185215
There are two options available, `destination` and `filename`. They are both
@@ -207,14 +237,18 @@ the file (`file`) to aid with the decision.
207237
Note that `req.body` might not have been fully populated yet. It depends on the
208238
order that the client transmits fields and files to the server.
209239

240+
For understanding the calling convention used in the callback (needing to pass
241+
null as the first param), refer to
242+
[Node.js error handling](https://www.joyent.com/node-js/production/design/errors)
243+
210244
#### `MemoryStorage`
211245

212246
The memory storage engine stores the files in memory as `Buffer` objects. It
213247
doesn't have any options.
214248

215249
```javascript
216-
var storage = multer.memoryStorage()
217-
var upload = multer({ storage: storage })
250+
const storage = multer.memoryStorage()
251+
const upload = multer({ storage: storage })
218252
```
219253

220254
When using memory storage, the file info will contain a field called
@@ -233,7 +267,7 @@ The following integer values are available:
233267
Key | Description | Default
234268
--- | --- | ---
235269
`fieldNameSize` | Max field name size | 100 bytes
236-
`fieldSize` | Max field value size | 1MB
270+
`fieldSize` | Max field value size (in bytes) | 1MB
237271
`fields` | Max number of non-file fields | Infinity
238272
`fileSize` | For multipart forms, the max file size (in bytes) | Infinity
239273
`files` | For multipart forms, the max number of file fields | Infinity
@@ -274,8 +308,8 @@ If you want to catch errors specifically from Multer, you can call the
274308
middleware function by yourself. Also, if you want to catch only [the Multer errors](https://github.com/expressjs/multer/blob/master/lib/multer-error.js), you can use the `MulterError` class that is attached to the `multer` object itself (e.g. `err instanceof multer.MulterError`).
275309

276310
```javascript
277-
var multer = require('multer')
278-
var upload = multer().single('avatar')
311+
const multer = require('multer')
312+
const upload = multer().single('avatar')
279313

280314
app.post('/profile', function (req, res) {
281315
upload(req, res, function (err) {

0 commit comments

Comments
 (0)