1 | # Multer [![Build Status](https://travis-ci.org/expressjs/multer.svg?branch=master)](https://travis-ci.org/expressjs/multer) [![NPM version](https://badge.fury.io/js/multer.svg)](https://badge.fury.io/js/multer) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
|
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 | ## Installation
|
9 |
|
10 | ```sh
|
11 | npm install --save multer
|
12 | ```
|
13 |
|
14 | ## Usage
|
15 |
|
16 | 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.
|
17 |
|
18 | Basic usage example:
|
19 |
|
20 | ```javascript
|
21 | var multer = require('multer')
|
22 | var express = require('express')
|
23 |
|
24 | var app = express()
|
25 | var upload = multer()
|
26 |
|
27 | app.post('/profile', upload.single('avatar'), function (req, res, next) {
|
28 | // req.file is the `avatar` file
|
29 | // req.body will hold the text fields, if there were any
|
30 | })
|
31 |
|
32 | app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
|
33 | // req.files is array of `photos` files
|
34 | // req.body will contain the text fields, if there were any
|
35 | })
|
36 |
|
37 | var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
|
38 | app.post('/cool-profile', cpUpload, function (req, res, next) {
|
39 | // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
|
40 | //
|
41 | // e.g.
|
42 | // req.files['avatar'][0] -> File
|
43 | // req.files['gallery'] -> Array
|
44 | //
|
45 | // req.body will contain the text fields, if there were any
|
46 | })
|
47 | ```
|
48 |
|
49 | In case you need to handle a text-only multipart form, you can use the `.none()` method, example:
|
50 |
|
51 | ```javascript
|
52 | var multer = require('multer')
|
53 | var express = require('express')
|
54 |
|
55 | var app = express()
|
56 | var upload = multer()
|
57 |
|
58 | app.post('/profile', upload.none(), function (req, res, next) {
|
59 | // req.body contains the text fields
|
60 | })
|
61 | ```
|
62 |
|
63 | ## API
|
64 |
|
65 | ### File information
|
66 |
|
67 | Each file contains the following information:
|
68 |
|
69 | Key | Description
|
70 | --- | ---
|
71 | `fieldName` | Field name specified in the form
|
72 | `originalName` | Name of the file on the user's computer (`undefined` if no filename was supplied by the client)
|
73 | `size` | Size of the file in bytes
|
74 | `stream` | Stream of file
|
75 | `detectedMimeType` | The detected mime-type, or null if we failed to detect
|
76 | `detectedFileExtension` | The typical file extension for files of the detected type, or empty string if we failed to detect (with leading `.` to match `path.extname`)
|
77 | `clientReportedMimeType` | The mime type reported by the client using the `Content-Type` header, or null<sup>1</sup> if the header was absent
|
78 | `clientReportedFileExtension` | The extension of the file uploaded (as reported by `path.extname`)
|
79 |
|
80 | <sup>1</sup> Currently returns `text/plain` if header is absent, this is a bug and it will be fixed in a patch release. Do not rely on this behavior.
|
81 |
|
82 | ### `multer(opts)`
|
83 |
|
84 | Multer accepts an options object, the following are the options that can be
|
85 | passed to Multer.
|
86 |
|
87 | Key | Description
|
88 | -------- | -----------
|
89 | `limits` | Limits of the uploaded data [(full description)](#limits)
|
90 |
|
91 | #### `.single(fieldname)`
|
92 |
|
93 | Accept a single file with the name `fieldname`. The single file will be stored
|
94 | in `req.file`.
|
95 |
|
96 | #### `.array(fieldname[, maxCount])`
|
97 |
|
98 | Accept an array of files, all with the name `fieldname`. Optionally error out if
|
99 | more than `maxCount` files are uploaded. The array of files will be stored in
|
100 | `req.files`.
|
101 |
|
102 | #### `.fields(fields)`
|
103 |
|
104 | Accept a mix of files, specified by `fields`. An object with arrays of files
|
105 | will be stored in `req.files`.
|
106 |
|
107 | `fields` should be an array of objects with `name` and optionally a `maxCount`.
|
108 | Example:
|
109 |
|
110 | ```javascript
|
111 | [
|
112 | { name: 'avatar', maxCount: 1 },
|
113 | { name: 'gallery', maxCount: 8 }
|
114 | ]
|
115 | ```
|
116 |
|
117 | #### `.none()`
|
118 |
|
119 | Accept only text fields. If any file upload is made, error with code
|
120 | "LIMIT\_UNEXPECTED\_FILE" will be issued. This is the same as doing `upload.fields([])`.
|
121 |
|
122 | #### `.any()`
|
123 |
|
124 | Accepts all files that comes over the wire. An array of files will be stored in
|
125 | `req.files`.
|
126 |
|
127 | **WARNING:** Make sure that you always handle the files that a user uploads.
|
128 | Never add multer as a global middleware since a malicious user could upload
|
129 | files to a route that you didn't anticipate. Only use this function on routes
|
130 | where you are handling the uploaded files.
|
131 |
|
132 | ### `limits`
|
133 |
|
134 | 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).
|
135 |
|
136 | The following integer values are available:
|
137 |
|
138 | Key | Description | Default
|
139 | --- | --- | ---
|
140 | `fieldNameSize` | Max field name size | 100 bytes
|
141 | `fieldSize` | Max field value size | 1MB
|
142 | `fields` | Max number of non-file fields | Infinity
|
143 | `fileSize` | For multipart forms, the max file size (in bytes) | Infinity
|
144 | `files` | For multipart forms, the max number of file fields | Infinity
|
145 | `parts` | For multipart forms, the max number of parts (fields + files) | Infinity
|
146 | `headerPairs` | For multipart forms, the max number of header key=>value pairs to parse | 2000
|
147 |
|
148 | Specifying the limits can help protect your site against denial of service (DoS) attacks.
|
149 |
|
150 | ## Error handling
|
151 |
|
152 | When encountering an error, multer will delegate the error to express. You can
|
153 | display a nice error page using [the standard express way](http://expressjs.com/guide/error-handling.html).
|
154 |
|
155 | If you want to catch errors specifically from multer, you can call the
|
156 | middleware function by yourself.
|
157 |
|
158 | ```javascript
|
159 | var upload = multer().single('avatar')
|
160 |
|
161 | app.post('/profile', function (req, res) {
|
162 | upload(req, res, function (err) {
|
163 | if (err) {
|
164 | // An error occurred when uploading
|
165 | return
|
166 | }
|
167 |
|
168 | // Everything went fine
|
169 | })
|
170 | })
|
171 | ```
|