root / HServer / 00.Server / 00.Program / node_modules / multer / README.md
이력 | 보기 | 이력해설 | 다운로드 (9.41 KB)
1 | 39 | HKM | # Multer [](https://travis-ci.org/expressjs/multer) [](https://badge.fury.io/js/multer) [](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 | ## Translations |
||
9 | |||
10 | This README is also available in other languages: |
||
11 | |||
12 | - [简体中文](https://github.com/expressjs/multer/blob/master/doc/README-zh-cn.md) (Chinese) |
||
13 | |||
14 | ## Installation |
||
15 | |||
16 | ```sh |
||
17 | $ npm install --save multer |
||
18 | ``` |
||
19 | |||
20 | ## Usage |
||
21 | |||
22 | 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. |
||
23 | |||
24 | Basic usage example: |
||
25 | |||
26 | ```javascript |
||
27 | var express = require('express') |
||
28 | var multer = require('multer') |
||
29 | var upload = multer({ dest: 'uploads/' }) |
||
30 | |||
31 | var app = express() |
||
32 | |||
33 | app.post('/profile', upload.single('avatar'), function (req, res, next) { |
||
34 | // req.file is the `avatar` file |
||
35 | // req.body will hold the text fields, if there were any |
||
36 | }) |
||
37 | |||
38 | app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) { |
||
39 | // req.files is array of `photos` files |
||
40 | // req.body will contain the text fields, if there were any |
||
41 | }) |
||
42 | |||
43 | var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]) |
||
44 | app.post('/cool-profile', cpUpload, function (req, res, next) { |
||
45 | // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files |
||
46 | // |
||
47 | // e.g. |
||
48 | // req.files['avatar'][0] -> File |
||
49 | // req.files['gallery'] -> Array |
||
50 | // |
||
51 | // req.body will contain the text fields, if there were any |
||
52 | }) |
||
53 | ``` |
||
54 | |||
55 | 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()`: |
||
56 | |||
57 | ```javascript |
||
58 | var express = require('express') |
||
59 | var app = express() |
||
60 | var multer = require('multer') |
||
61 | var upload = multer() |
||
62 | |||
63 | app.post('/profile', upload.array(), function (req, res, next) { |
||
64 | // req.body contains the text fields |
||
65 | }) |
||
66 | ``` |
||
67 | |||
68 | ## API |
||
69 | |||
70 | ### File information |
||
71 | |||
72 | Each file contains the following information: |
||
73 | |||
74 | Key | Description | Note |
||
75 | --- | --- | --- |
||
76 | `fieldname` | Field name specified in the form | |
||
77 | `originalname` | Name of the file on the user's computer | |
||
78 | `encoding` | Encoding type of the file | |
||
79 | `mimetype` | Mime type of the file | |
||
80 | `size` | Size of the file in bytes | |
||
81 | `destination` | The folder to which the file has been saved | `DiskStorage` |
||
82 | `filename` | The name of the file within the `destination` | `DiskStorage` |
||
83 | `path` | The full path to the uploaded file | `DiskStorage` |
||
84 | `buffer` | A `Buffer` of the entire file | `MemoryStorage` |
||
85 | |||
86 | ### `multer(opts)` |
||
87 | |||
88 | Multer accepts an options object, the most basic of which is the `dest` |
||
89 | property, which tells Multer where to upload the files. In case you omit the |
||
90 | options object, the files will be kept in memory and never written to disk. |
||
91 | |||
92 | By default, Multer will rename the files so as to avoid naming conflicts. The |
||
93 | renaming function can be customized according to your needs. |
||
94 | |||
95 | The following are the options that can be passed to Multer. |
||
96 | |||
97 | Key | Description |
||
98 | --- | --- |
||
99 | `dest` or `storage` | Where to store the files |
||
100 | `fileFilter` | Function to control which files are accepted |
||
101 | `limits` | Limits of the uploaded data |
||
102 | `preservePath` | Keep the full path of files instead of just the base name |
||
103 | |||
104 | In an average web app, only `dest` might be required, and configured as shown in |
||
105 | the following example. |
||
106 | |||
107 | ```javascript |
||
108 | var upload = multer({ dest: 'uploads/' }) |
||
109 | ``` |
||
110 | |||
111 | If you want more control over your uploads, you'll want to use the `storage` |
||
112 | option instead of `dest`. Multer ships with storage engines `DiskStorage` |
||
113 | and `MemoryStorage`; More engines are available from third parties. |
||
114 | |||
115 | #### `.single(fieldname)` |
||
116 | |||
117 | Accept a single file with the name `fieldname`. The single file will be stored |
||
118 | in `req.file`. |
||
119 | |||
120 | #### `.array(fieldname[, maxCount])` |
||
121 | |||
122 | Accept an array of files, all with the name `fieldname`. Optionally error out if |
||
123 | more than `maxCount` files are uploaded. The array of files will be stored in |
||
124 | `req.files`. |
||
125 | |||
126 | #### `.fields(fields)` |
||
127 | |||
128 | Accept a mix of files, specified by `fields`. An object with arrays of files |
||
129 | will be stored in `req.files`. |
||
130 | |||
131 | `fields` should be an array of objects with `name` and optionally a `maxCount`. |
||
132 | Example: |
||
133 | |||
134 | ```javascript |
||
135 | [ |
||
136 | { name: 'avatar', maxCount: 1 }, |
||
137 | { name: 'gallery', maxCount: 8 } |
||
138 | ] |
||
139 | ``` |
||
140 | |||
141 | #### `.none()` |
||
142 | |||
143 | Accept only text fields. If any file upload is made, error with code |
||
144 | "LIMIT\_UNEXPECTED\_FILE" will be issued. This is the same as doing `upload.fields([])`. |
||
145 | |||
146 | #### `.any()` |
||
147 | |||
148 | Accepts all files that comes over the wire. An array of files will be stored in |
||
149 | `req.files`. |
||
150 | |||
151 | **WARNING:** Make sure that you always handle the files that a user uploads. |
||
152 | Never add multer as a global middleware since a malicious user could upload |
||
153 | files to a route that you didn't anticipate. Only use this function on routes |
||
154 | where you are handling the uploaded files. |
||
155 | |||
156 | ### `storage` |
||
157 | |||
158 | #### `DiskStorage` |
||
159 | |||
160 | The disk storage engine gives you full control on storing files to disk. |
||
161 | |||
162 | ```javascript |
||
163 | var storage = multer.diskStorage({ |
||
164 | destination: function (req, file, cb) { |
||
165 | cb(null, '/tmp/my-uploads') |
||
166 | }, |
||
167 | filename: function (req, file, cb) { |
||
168 | cb(null, file.fieldname + '-' + Date.now()) |
||
169 | } |
||
170 | }) |
||
171 | |||
172 | var upload = multer({ storage: storage }) |
||
173 | ``` |
||
174 | |||
175 | There are two options available, `destination` and `filename`. They are both |
||
176 | functions that determine where the file should be stored. |
||
177 | |||
178 | `destination` is used to determine within which folder the uploaded files should |
||
179 | be stored. This can also be given as a `string` (e.g. `'/tmp/uploads'`). If no |
||
180 | `destination` is given, the operating system's default directory for temporary |
||
181 | files is used. |
||
182 | |||
183 | **Note:** You are responsible for creating the directory when providing |
||
184 | `destination` as a function. When passing a string, multer will make sure that |
||
185 | the directory is created for you. |
||
186 | |||
187 | `filename` is used to determine what the file should be named inside the folder. |
||
188 | If no `filename` is given, each file will be given a random name that doesn't |
||
189 | include any file extension. |
||
190 | |||
191 | **Note:** Multer will not append any file extension for you, your function |
||
192 | should return a filename complete with an file extension. |
||
193 | |||
194 | Each function gets passed both the request (`req`) and some information about |
||
195 | the file (`file`) to aid with the decision. |
||
196 | |||
197 | Note that `req.body` might not have been fully populated yet. It depends on the |
||
198 | order that the client transmits fields and files to the server. |
||
199 | |||
200 | #### `MemoryStorage` |
||
201 | |||
202 | The memory storage engine stores the files in memory as `Buffer` objects. It |
||
203 | doesn't have any options. |
||
204 | |||
205 | ```javascript |
||
206 | var storage = multer.memoryStorage() |
||
207 | var upload = multer({ storage: storage }) |
||
208 | ``` |
||
209 | |||
210 | When using memory storage, the file info will contain a field called |
||
211 | `buffer` that contains the entire file. |
||
212 | |||
213 | **WARNING**: Uploading very large files, or relatively small files in large |
||
214 | numbers very quickly, can cause your application to run out of memory when |
||
215 | memory storage is used. |
||
216 | |||
217 | ### `limits` |
||
218 | |||
219 | 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). |
||
220 | |||
221 | The following integer values are available: |
||
222 | |||
223 | Key | Description | Default |
||
224 | --- | --- | --- |
||
225 | `fieldNameSize` | Max field name size | 100 bytes |
||
226 | `fieldSize` | Max field value size | 1MB |
||
227 | `fields` | Max number of non-file fields | Infinity |
||
228 | `fileSize` | For multipart forms, the max file size (in bytes) | Infinity |
||
229 | `files` | For multipart forms, the max number of file fields | Infinity |
||
230 | `parts` | For multipart forms, the max number of parts (fields + files) | Infinity |
||
231 | `headerPairs` | For multipart forms, the max number of header key=>value pairs to parse | 2000 |
||
232 | |||
233 | Specifying the limits can help protect your site against denial of service (DoS) attacks. |
||
234 | |||
235 | ### `fileFilter` |
||
236 | |||
237 | Set this to a function to control which files should be uploaded and which |
||
238 | should be skipped. The function should look like this: |
||
239 | |||
240 | ```javascript |
||
241 | function fileFilter (req, file, cb) { |
||
242 | |||
243 | // The function should call `cb` with a boolean |
||
244 | // to indicate if the file should be accepted |
||
245 | |||
246 | // To reject this file pass `false`, like so: |
||
247 | cb(null, false) |
||
248 | |||
249 | // To accept the file pass `true`, like so: |
||
250 | cb(null, true) |
||
251 | |||
252 | // You can always pass an error if something goes wrong: |
||
253 | cb(new Error('I don\'t have a clue!')) |
||
254 | |||
255 | } |
||
256 | ``` |
||
257 | |||
258 | ## Error handling |
||
259 | |||
260 | When encountering an error, multer will delegate the error to express. You can |
||
261 | display a nice error page using [the standard express way](http://expressjs.com/guide/error-handling.html). |
||
262 | |||
263 | If you want to catch errors specifically from multer, you can call the |
||
264 | middleware function by yourself. |
||
265 | |||
266 | ```javascript |
||
267 | var upload = multer().single('avatar') |
||
268 | |||
269 | app.post('/profile', function (req, res) { |
||
270 | upload(req, res, function (err) { |
||
271 | if (err) { |
||
272 | // An error occurred when uploading |
||
273 | return |
||
274 | } |
||
275 | |||
276 | // Everything went fine |
||
277 | }) |
||
278 | }) |
||
279 | ``` |
||
280 | |||
281 | ## Custom storage engine |
||
282 | |||
283 | For information on how to build your own storage engine, see [Multer Storage Engine](https://github.com/expressjs/multer/blob/master/StorageEngine.md). |
||
284 | |||
285 | ## License |
||
286 | |||
287 | [MIT](LICENSE) |