root / HServer / 00.Server / 00.Program / node_modules / multiparty / README.md
이력 | 보기 | 이력해설 | 다운로드 (8.2 KB)
1 | 39 | HKM | # multiparty [](https://travis-ci.org/pillarjs/multiparty) [](https://coveralls.io/r/pillarjs/multiparty) |
---|---|---|---|
2 | |||
3 | Parse http requests with content-type `multipart/form-data`, also known as file uploads. |
||
4 | |||
5 | See also [busboy](https://github.com/mscdex/busboy) - a |
||
6 | [faster](https://github.com/mscdex/dicer/wiki/Benchmarks) alternative |
||
7 | which may be worth looking into. |
||
8 | |||
9 | ### Why the fork? |
||
10 | |||
11 | * This module uses the Node.js v0.10 streams properly |
||
12 | * It will not create a temp file for you unless you want it to. |
||
13 | * Counts bytes and does math to help you figure out the `Content-Length` of |
||
14 | the final part. |
||
15 | * You can stream uploads to s3 with |
||
16 | [aws-sdk](https://github.com/aws/aws-sdk-js), for [example](examples/s3.js). |
||
17 | * Less bugs. This code is simpler, has all deprecated functionality removed, |
||
18 | has cleaner tests, and does not try to do anything beyond multipart stream |
||
19 | parsing. |
||
20 | |||
21 | ## Installation |
||
22 | |||
23 | ``` |
||
24 | npm install multiparty |
||
25 | ``` |
||
26 | |||
27 | ## Usage |
||
28 | |||
29 | * See [examples](examples). |
||
30 | |||
31 | Parse an incoming `multipart/form-data` request. |
||
32 | |||
33 | ```js |
||
34 | var multiparty = require('multiparty'); |
||
35 | var http = require('http'); |
||
36 | var util = require('util'); |
||
37 | |||
38 | http.createServer(function(req, res) { |
||
39 | if (req.url === '/upload' && req.method === 'POST') { |
||
40 | // parse a file upload |
||
41 | var form = new multiparty.Form(); |
||
42 | |||
43 | form.parse(req, function(err, fields, files) { |
||
44 | res.writeHead(200, {'content-type': 'text/plain'}); |
||
45 | res.write('received upload:\n\n'); |
||
46 | res.end(util.inspect({fields: fields, files: files})); |
||
47 | }); |
||
48 | |||
49 | return; |
||
50 | } |
||
51 | |||
52 | // show a file upload form |
||
53 | res.writeHead(200, {'content-type': 'text/html'}); |
||
54 | res.end( |
||
55 | '<form action="/upload" enctype="multipart/form-data" method="post">'+ |
||
56 | '<input type="text" name="title"><br>'+ |
||
57 | '<input type="file" name="upload" multiple="multiple"><br>'+ |
||
58 | '<input type="submit" value="Upload">'+ |
||
59 | '</form>' |
||
60 | ); |
||
61 | }).listen(8080); |
||
62 | ``` |
||
63 | |||
64 | ## API |
||
65 | |||
66 | ### multiparty.Form |
||
67 | |||
68 | ```js |
||
69 | var form = new multiparty.Form(options) |
||
70 | ``` |
||
71 | |||
72 | Creates a new form. Options: |
||
73 | |||
74 | * `encoding` - sets encoding for the incoming form fields. Defaults to `utf8`. |
||
75 | * `maxFieldsSize` - Limits the amount of memory all fields (not files) can |
||
76 | allocate in bytes. If this value is exceeded, an `error` event is emitted. |
||
77 | The default size is 2MB. |
||
78 | * `maxFields` - Limits the number of fields that will be parsed before |
||
79 | emitting an `error` event. A file counts as a field in this case. |
||
80 | Defaults to 1000. |
||
81 | * `maxFilesSize` - Only relevant when `autoFiles` is `true`. Limits the |
||
82 | total bytes accepted for all files combined. If this value is exceeded, |
||
83 | an `error` event is emitted. The default is `Infinity`. |
||
84 | * `autoFields` - Enables `field` events and disables `part` events for fields. |
||
85 | This is automatically set to `true` if you add a `field` listener. |
||
86 | * `autoFiles` - Enables `file` events and disables `part` events for files. |
||
87 | This is automatically set to `true` if you add a `file` listener. |
||
88 | * `uploadDir` - Only relevant when `autoFiles` is `true`. The directory for |
||
89 | placing file uploads in. You can move them later using `fs.rename()`. |
||
90 | Defaults to `os.tmpdir()`. |
||
91 | |||
92 | #### form.parse(request, [cb]) |
||
93 | |||
94 | Parses an incoming node.js `request` containing form data.This will cause |
||
95 | `form` to emit events based off the incoming request. |
||
96 | |||
97 | ```js |
||
98 | var count = 0; |
||
99 | var form = new multiparty.Form(); |
||
100 | |||
101 | // Errors may be emitted |
||
102 | // Note that if you are listening to 'part' events, the same error may be |
||
103 | // emitted from the `form` and the `part`. |
||
104 | form.on('error', function(err) { |
||
105 | console.log('Error parsing form: ' + err.stack); |
||
106 | }); |
||
107 | |||
108 | // Parts are emitted when parsing the form |
||
109 | form.on('part', function(part) { |
||
110 | // You *must* act on the part by reading it |
||
111 | // NOTE: if you want to ignore it, just call "part.resume()" |
||
112 | |||
113 | if (!part.filename) { |
||
114 | // filename is not defined when this is a field and not a file |
||
115 | console.log('got field named ' + part.name); |
||
116 | // ignore field's content |
||
117 | part.resume(); |
||
118 | } |
||
119 | |||
120 | if (part.filename) { |
||
121 | // filename is defined when this is a file |
||
122 | count++; |
||
123 | console.log('got file named ' + part.name); |
||
124 | // ignore file's content here |
||
125 | part.resume(); |
||
126 | } |
||
127 | |||
128 | part.on('error', function(err) { |
||
129 | // decide what to do |
||
130 | }); |
||
131 | }); |
||
132 | |||
133 | // Close emitted after form parsed |
||
134 | form.on('close', function() { |
||
135 | console.log('Upload completed!'); |
||
136 | res.setHeader('text/plain'); |
||
137 | res.end('Received ' + count + ' files'); |
||
138 | }); |
||
139 | |||
140 | // Parse req |
||
141 | form.parse(req); |
||
142 | ``` |
||
143 | |||
144 | If `cb` is provided, `autoFields` and `autoFiles` are set to `true` and all |
||
145 | fields and files are collected and passed to the callback, removing the need to |
||
146 | listen to any events on `form`. This is for convenience when you want to read |
||
147 | everything, but be sure to write cleanup code, as this will write all uploaded |
||
148 | files to the disk, even ones you may not be interested in. |
||
149 | |||
150 | ```js |
||
151 | form.parse(req, function(err, fields, files) { |
||
152 | Object.keys(fields).forEach(function(name) { |
||
153 | console.log('got field named ' + name); |
||
154 | }); |
||
155 | |||
156 | Object.keys(files).forEach(function(name) { |
||
157 | console.log('got file named ' + name); |
||
158 | }); |
||
159 | |||
160 | console.log('Upload completed!'); |
||
161 | res.setHeader('text/plain'); |
||
162 | res.end('Received ' + files.length + ' files'); |
||
163 | }); |
||
164 | ``` |
||
165 | |||
166 | `fields` is an object where the property names are field names and the values |
||
167 | are arrays of field values. |
||
168 | |||
169 | `files` is an object where the property names are field names and the values |
||
170 | are arrays of file objects. |
||
171 | |||
172 | #### form.bytesReceived |
||
173 | |||
174 | The amount of bytes received for this form so far. |
||
175 | |||
176 | #### form.bytesExpected |
||
177 | |||
178 | The expected number of bytes in this form. |
||
179 | |||
180 | ### Events |
||
181 | |||
182 | #### 'error' (err) |
||
183 | |||
184 | Unless you supply a callback to `form.parse`, you definitely want to handle |
||
185 | this event. Otherwise your server *will* crash when users submit bogus |
||
186 | multipart requests! |
||
187 | |||
188 | Only one 'error' event can ever be emitted, and if an 'error' event is |
||
189 | emitted, then 'close' will not be emitted. |
||
190 | |||
191 | If the error would correspond to a certain HTTP response code, the `err` object |
||
192 | will have a `statusCode` property with the value of the suggested HTTP response |
||
193 | code to send back. |
||
194 | |||
195 | Note that an 'error' event will be emitted both from the `form` and from the |
||
196 | current `part`. |
||
197 | |||
198 | #### 'part' (part) |
||
199 | |||
200 | Emitted when a part is encountered in the request. `part` is a |
||
201 | `ReadableStream`. It also has the following properties: |
||
202 | |||
203 | * `headers` - the headers for this part. For example, you may be interested |
||
204 | in `content-type`. |
||
205 | * `name` - the field name for this part |
||
206 | * `filename` - only if the part is an incoming file |
||
207 | * `byteOffset` - the byte offset of this part in the request body |
||
208 | * `byteCount` - assuming that this is the last part in the request, |
||
209 | this is the size of this part in bytes. You could use this, for |
||
210 | example, to set the `Content-Length` header if uploading to S3. |
||
211 | If the part had a `Content-Length` header then that value is used |
||
212 | here instead. |
||
213 | |||
214 | Parts for fields are not emitted when `autoFields` is on, and likewise parts |
||
215 | for files are not emitted when `autoFiles` is on. |
||
216 | |||
217 | `part` emits 'error' events! Make sure you handle them. |
||
218 | |||
219 | #### 'aborted' |
||
220 | |||
221 | Emitted when the request is aborted. This event will be followed shortly |
||
222 | by an `error` event. In practice you do not need to handle this event. |
||
223 | |||
224 | #### 'progress' (bytesReceived, bytesExpected) |
||
225 | |||
226 | #### 'close' |
||
227 | |||
228 | Emitted after all parts have been parsed and emitted. Not emitted if an `error` |
||
229 | event is emitted. |
||
230 | |||
231 | If you have `autoFiles` on, this is not fired until all the data has been |
||
232 | flushed to disk and the file handles have been closed. |
||
233 | |||
234 | This is typically when you would send your response. |
||
235 | |||
236 | #### 'file' (name, file) |
||
237 | |||
238 | **By default multiparty will not touch your hard drive.** But if you add this |
||
239 | listener, multiparty automatically sets `form.autoFiles` to `true` and will |
||
240 | stream uploads to disk for you. |
||
241 | |||
242 | **The max bytes accepted per request can be specified with `maxFilesSize`.** |
||
243 | |||
244 | * `name` - the field name for this file |
||
245 | * `file` - an object with these properties: |
||
246 | - `fieldName` - same as `name` - the field name for this file |
||
247 | - `originalFilename` - the filename that the user reports for the file |
||
248 | - `path` - the absolute path of the uploaded file on disk |
||
249 | - `headers` - the HTTP headers that were sent along with this file |
||
250 | - `size` - size of the file in bytes |
||
251 | |||
252 | #### 'field' (name, value) |
||
253 | |||
254 | * `name` - field name |
||
255 | * `value` - string field value |