Skip to content

Commit 5d2ed4c

Browse files
authored
Merge branch 'master' into master
2 parents ad6de14 + dd3ecef commit 5d2ed4c

25 files changed

+891
-294
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
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.3 - 2021-08-09
7+
8+
- Bugfix: Avoid deprecated pseudoRandomBytes function (#774)
9+
- Docs: Add Português Brazil translation for README (#758)
10+
- Docs: Clarify the callback calling convention (#775)
11+
- Docs: Add example on how to link to html multipart form (#580)
12+
- Docs: Add Spanish translation for README (#838)
13+
- Docs: Add Math.random() to storage filename example (#841)
14+
- Docs: Fix mistakes in russian doc (#869)
15+
- Docs: Improve Português Brazil translation (#877)
16+
- Docs: Update var to const in all Readmes (#1024)
17+
- Internal: Bump mkdirp version (#862)
18+
- Internal: Bump Standard version (#878)
19+
620
## 1.4.2 - 2019-07-16
721

822
- Docs: Add Russian translation for README (#662)

README.md

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +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)
1516
- [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)
1618

1719
## Installation
1820

@@ -35,11 +37,11 @@ Don't forget the `enctype="multipart/form-data"` in your form.
3537
```
3638

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

42-
var app = express()
44+
const app = express()
4345

4446
app.post('/profile', upload.single('avatar'), function (req, res, next) {
4547
// req.file is the `avatar` file
@@ -51,7 +53,7 @@ app.post('/photos/upload', upload.array('photos', 12), function (req, res, next)
5153
// req.body will contain the text fields, if there were any
5254
})
5355

54-
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 }])
5557
app.post('/cool-profile', cpUpload, function (req, res, next) {
5658
// req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
5759
//
@@ -66,16 +68,42 @@ app.post('/cool-profile', cpUpload, function (req, res, next) {
6668
In case you need to handle a text-only multipart form, you should use the `.none()` method:
6769

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

7476
app.post('/profile', upload.none(), function (req, res, next) {
7577
// req.body contains the text fields
7678
})
7779
```
7880

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+
79107
## API
80108

81109
### File information
@@ -116,7 +144,7 @@ In an average web app, only `dest` might be required, and configured as shown in
116144
the following example.
117145

118146
```javascript
119-
var upload = multer({ dest: 'uploads/' })
147+
const upload = multer({ dest: 'uploads/' })
120148
```
121149

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

173201
```javascript
174-
var storage = multer.diskStorage({
202+
const storage = multer.diskStorage({
175203
destination: function (req, file, cb) {
176204
cb(null, '/tmp/my-uploads')
177205
},
178206
filename: function (req, file, cb) {
179-
cb(null, file.fieldname + '-' + Date.now())
207+
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
208+
cb(null, file.fieldname + '-' + uniqueSuffix)
180209
}
181210
})
182211

183-
var upload = multer({ storage: storage })
212+
const upload = multer({ storage: storage })
184213
```
185214

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

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+
211244
#### `MemoryStorage`
212245

213246
The memory storage engine stores the files in memory as `Buffer` objects. It
214247
doesn't have any options.
215248

216249
```javascript
217-
var storage = multer.memoryStorage()
218-
var upload = multer({ storage: storage })
250+
const storage = multer.memoryStorage()
251+
const upload = multer({ storage: storage })
219252
```
220253

221254
When using memory storage, the file info will contain a field called
@@ -275,8 +308,8 @@ If you want to catch errors specifically from Multer, you can call the
275308
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`).
276309

277310
```javascript
278-
var multer = require('multer')
279-
var upload = multer().single('avatar')
311+
const multer = require('multer')
312+
const upload = multer().single('avatar')
280313

281314
app.post('/profile', function (req, res) {
282315
upload(req, res, function (err) {

0 commit comments

Comments
 (0)