PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/formidable/Readme.md

https://gitlab.com/nhanvu/cyber-duck
Markdown | 425 lines | 320 code | 105 blank | 0 comment | 0 complexity | b93a4007ab1e86024189eceb02e71db8 MD5 | raw file
  1. # Formidable
  2. [![Build Status](https://secure.travis-ci.org/felixge/node-formidable.png?branch=master)](http://travis-ci.org/felixge/node-formidable)
  3. ## Purpose
  4. A node.js module for parsing form data, especially file uploads.
  5. ## Current status
  6. This module was developed for [Transloadit](http://transloadit.com/), a service focused on uploading
  7. and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from
  8. a large variety of clients and is considered production-ready.
  9. ## Features
  10. * Fast (~500mb/sec), non-buffering multipart parser
  11. * Automatically writing file uploads to disk
  12. * Low memory footprint
  13. * Graceful error handling
  14. * Very high test coverage
  15. ## Installation
  16. This is a low level package, and if you're using a high level framework such as Express, chances are it's already included in it. You can [read this discussion](http://stackoverflow.com/questions/11295554/how-to-disable-express-bodyparser-for-file-uploads-node-js) about how Formidable is integrated with Express.
  17. Via [npm](http://github.com/isaacs/npm):
  18. ```
  19. npm install formidable@latest
  20. ```
  21. Manually:
  22. ```
  23. git clone git://github.com/felixge/node-formidable.git formidable
  24. vim my.js
  25. # var formidable = require('./formidable');
  26. ```
  27. Note: Formidable requires [gently](http://github.com/felixge/node-gently) to run the unit tests, but you won't need it for just using the library.
  28. ## Example
  29. Parse an incoming file upload.
  30. ```javascript
  31. var formidable = require('formidable'),
  32. http = require('http'),
  33. util = require('util');
  34. http.createServer(function(req, res) {
  35. if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
  36. // parse a file upload
  37. var form = new formidable.IncomingForm();
  38. form.parse(req, function(err, fields, files) {
  39. res.writeHead(200, {'content-type': 'text/plain'});
  40. res.write('received upload:\n\n');
  41. res.end(util.inspect({fields: fields, files: files}));
  42. });
  43. return;
  44. }
  45. // show a file upload form
  46. res.writeHead(200, {'content-type': 'text/html'});
  47. res.end(
  48. '<form action="/upload" enctype="multipart/form-data" method="post">'+
  49. '<input type="text" name="title"><br>'+
  50. '<input type="file" name="upload" multiple="multiple"><br>'+
  51. '<input type="submit" value="Upload">'+
  52. '</form>'
  53. );
  54. }).listen(8080);
  55. ```
  56. ## API
  57. ### Formidable.IncomingForm
  58. ```javascript
  59. var form = new formidable.IncomingForm()
  60. ```
  61. Creates a new incoming form.
  62. ```javascript
  63. form.encoding = 'utf-8';
  64. ```
  65. Sets encoding for incoming form fields.
  66. ```javascript
  67. form.uploadDir = "/my/dir";
  68. ```
  69. Sets the directory for placing file uploads in. You can move them later on using
  70. `fs.rename()`. The default is `os.tmpDir()`.
  71. ```javascript
  72. form.keepExtensions = false;
  73. ```
  74. If you want the files written to `form.uploadDir` to include the extensions of the original files, set this property to `true`.
  75. ```javascript
  76. form.type
  77. ```
  78. Either 'multipart' or 'urlencoded' depending on the incoming request.
  79. ```javascript
  80. form.maxFieldsSize = 2 * 1024 * 1024;
  81. ```
  82. Limits the amount of memory all fields together (except files) can allocate in bytes.
  83. If this value is exceeded, an `'error'` event is emitted. The default
  84. size is 2MB.
  85. ```javascript
  86. form.maxFields = 1000;
  87. ```
  88. Limits the number of fields that the querystring parser will decode. Defaults
  89. to 1000 (0 for unlimited).
  90. ```javascript
  91. form.hash = false;
  92. ```
  93. If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.
  94. ```javascript
  95. form.multiples = false;
  96. ```
  97. If this option is enabled, when you call `form.parse`, the `files` argument will contain arrays of files for inputs which submit multiple files using the HTML5 `multiple` attribute.
  98. ```javascript
  99. form.bytesReceived
  100. ```
  101. The amount of bytes received for this form so far.
  102. ```javascript
  103. form.bytesExpected
  104. ```
  105. The expected number of bytes in this form.
  106. ```javascript
  107. form.parse(request, [cb]);
  108. ```
  109. Parses an incoming node.js `request` containing form data. If `cb` is provided, all fields and files are collected and passed to the callback:
  110. ```javascript
  111. form.parse(req, function(err, fields, files) {
  112. // ...
  113. });
  114. form.onPart(part);
  115. ```
  116. You may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any `'field'` / `'file'` events processing which would occur otherwise, making you fully responsible for handling the processing.
  117. ```javascript
  118. form.onPart = function(part) {
  119. part.addListener('data', function() {
  120. // ...
  121. });
  122. }
  123. ```
  124. If you want to use formidable to only handle certain parts for you, you can do so:
  125. ```javascript
  126. form.onPart = function(part) {
  127. if (!part.filename) {
  128. // let formidable handle all non-file parts
  129. form.handlePart(part);
  130. }
  131. }
  132. ```
  133. Check the code in this method for further inspiration.
  134. ### Formidable.File
  135. ```javascript
  136. file.size = 0
  137. ```
  138. The size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'` event), this property says how many bytes of the file have been written to disk yet.
  139. ```javascript
  140. file.path = null
  141. ```
  142. The path this file is being written to. You can modify this in the `'fileBegin'` event in
  143. case you are unhappy with the way formidable generates a temporary path for your files.
  144. ```javascript
  145. file.name = null
  146. ```
  147. The name this file had according to the uploading client.
  148. ```javascript
  149. file.type = null
  150. ```
  151. The mime type of this file, according to the uploading client.
  152. ```javascript
  153. file.lastModifiedDate = null
  154. ```
  155. A date object (or `null`) containing the time this file was last written to. Mostly
  156. here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
  157. ```javascript
  158. file.hash = null
  159. ```
  160. If hash calculation was set, you can read the hex digest out of this var.
  161. #### Formidable.File#toJSON()
  162. This method returns a JSON-representation of the file, allowing you to
  163. `JSON.stringify()` the file which is useful for logging and responding
  164. to requests.
  165. ### Events
  166. #### 'progress'
  167. ```javascript
  168. form.on('progress', function(bytesReceived, bytesExpected) {
  169. });
  170. ```
  171. Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
  172. #### 'field'
  173. ```javascript
  174. form.on('field', function(name, value) {
  175. });
  176. ```
  177. #### 'fileBegin'
  178. Emitted whenever a field / value pair has been received.
  179. ```javascript
  180. form.on('fileBegin', function(name, file) {
  181. });
  182. ```
  183. #### 'file'
  184. Emitted whenever a new file is detected in the upload stream. Use this even if
  185. you want to stream the file to somewhere else while buffering the upload on
  186. the file system.
  187. Emitted whenever a field / file pair has been received. `file` is an instance of `File`.
  188. ```javascript
  189. form.on('file', function(name, file) {
  190. });
  191. ```
  192. #### 'error'
  193. Emitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call `request.resume()` if you want the request to continue firing `'data'` events.
  194. ```javascript
  195. form.on('error', function(err) {
  196. });
  197. ```
  198. #### 'aborted'
  199. Emitted when the request was aborted by the user. Right now this can be due to a 'timeout' or 'close' event on the socket. After this event is emitted, an `error` event will follow. In the future there will be a separate 'timeout' event (needs a change in the node core).
  200. ```javascript
  201. form.on('aborted', function() {
  202. });
  203. ```
  204. ##### 'end'
  205. ```javascript
  206. form.on('end', function() {
  207. });
  208. ```
  209. Emitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response.
  210. ## Changelog
  211. ### v1.0.14
  212. * Add failing hash tests. (Ben Trask)
  213. * Enable hash calculation again (Eugene Girshov)
  214. * Test for immediate data events (Tim Smart)
  215. * Re-arrange IncomingForm#parse (Tim Smart)
  216. ### v1.0.13
  217. * Only update hash if update method exists (Sven Lito)
  218. * According to travis v0.10 needs to go quoted (Sven Lito)
  219. * Bumping build node versions (Sven Lito)
  220. * Additional fix for empty requests (Eugene Girshov)
  221. * Change the default to 1000, to match the new Node behaviour. (OrangeDog)
  222. * Add ability to control maxKeys in the querystring parser. (OrangeDog)
  223. * Adjust test case to work with node 0.9.x (Eugene Girshov)
  224. * Update package.json (Sven Lito)
  225. * Path adjustment according to eb4468b (Markus Ast)
  226. ### v1.0.12
  227. * Emit error on aborted connections (Eugene Girshov)
  228. * Add support for empty requests (Eugene Girshov)
  229. * Fix name/filename handling in Content-Disposition (jesperp)
  230. * Tolerate malformed closing boundary in multipart (Eugene Girshov)
  231. * Ignore preamble in multipart messages (Eugene Girshov)
  232. * Add support for application/json (Mike Frey, Carlos Rodriguez)
  233. * Add support for Base64 encoding (Elmer Bulthuis)
  234. * Add File#toJSON (TJ Holowaychuk)
  235. * Remove support for Node.js 0.4 & 0.6 (Andrew Kelley)
  236. * Documentation improvements (Sven Lito, Andre Azevedo)
  237. * Add support for application/octet-stream (Ion Lupascu, Chris Scribner)
  238. * Use os.tmpDir() to get tmp directory (Andrew Kelley)
  239. * Improve package.json (Andrew Kelley, Sven Lito)
  240. * Fix benchmark script (Andrew Kelley)
  241. * Fix scope issue in incoming_forms (Sven Lito)
  242. * Fix file handle leak on error (OrangeDog)
  243. ### v1.0.11
  244. * Calculate checksums for incoming files (sreuter)
  245. * Add definition parameters to "IncomingForm" as an argument (Math-)
  246. ### v1.0.10
  247. * Make parts to be proper Streams (Matt Robenolt)
  248. ### v1.0.9
  249. * Emit progress when content length header parsed (Tim Koschützki)
  250. * Fix Readme syntax due to GitHub changes (goob)
  251. * Replace references to old 'sys' module in Readme with 'util' (Peter Sugihara)
  252. ### v1.0.8
  253. * Strip potentially unsafe characters when using `keepExtensions: true`.
  254. * Switch to utest / urun for testing
  255. * Add travis build
  256. ### v1.0.7
  257. * Remove file from package that was causing problems when installing on windows. (#102)
  258. * Fix typos in Readme (Jason Davies).
  259. ### v1.0.6
  260. * Do not default to the default to the field name for file uploads where
  261. filename="".
  262. ### v1.0.5
  263. * Support filename="" in multipart parts
  264. * Explain unexpected end() errors in parser better
  265. **Note:** Starting with this version, formidable emits 'file' events for empty
  266. file input fields. Previously those were incorrectly emitted as regular file
  267. input fields with value = "".
  268. ### v1.0.4
  269. * Detect a good default tmp directory regardless of platform. (#88)
  270. ### v1.0.3
  271. * Fix problems with utf8 characters (#84) / semicolons in filenames (#58)
  272. * Small performance improvements
  273. * New test suite and fixture system
  274. ### v1.0.2
  275. * Exclude node\_modules folder from git
  276. * Implement new `'aborted'` event
  277. * Fix files in example folder to work with recent node versions
  278. * Make gently a devDependency
  279. [See Commits](https://github.com/felixge/node-formidable/compare/v1.0.1...v1.0.2)
  280. ### v1.0.1
  281. * Fix package.json to refer to proper main directory. (#68, Dean Landolt)
  282. [See Commits](https://github.com/felixge/node-formidable/compare/v1.0.0...v1.0.1)
  283. ### v1.0.0
  284. * Add support for multipart boundaries that are quoted strings. (Jeff Craig)
  285. This marks the beginning of development on version 2.0 which will include
  286. several architectural improvements.
  287. [See Commits](https://github.com/felixge/node-formidable/compare/v0.9.11...v1.0.0)
  288. ### v0.9.11
  289. * Emit `'progress'` event when receiving data, regardless of parsing it. (Tim Koschützki)
  290. * Use [W3C FileAPI Draft](http://dev.w3.org/2006/webapi/FileAPI/) properties for File class
  291. **Important:** The old property names of the File class will be removed in a
  292. future release.
  293. [See Commits](https://github.com/felixge/node-formidable/compare/v0.9.10...v0.9.11)
  294. ### Older releases
  295. These releases were done before starting to maintain the above Changelog:
  296. * [v0.9.10](https://github.com/felixge/node-formidable/compare/v0.9.9...v0.9.10)
  297. * [v0.9.9](https://github.com/felixge/node-formidable/compare/v0.9.8...v0.9.9)
  298. * [v0.9.8](https://github.com/felixge/node-formidable/compare/v0.9.7...v0.9.8)
  299. * [v0.9.7](https://github.com/felixge/node-formidable/compare/v0.9.6...v0.9.7)
  300. * [v0.9.6](https://github.com/felixge/node-formidable/compare/v0.9.5...v0.9.6)
  301. * [v0.9.5](https://github.com/felixge/node-formidable/compare/v0.9.4...v0.9.5)
  302. * [v0.9.4](https://github.com/felixge/node-formidable/compare/v0.9.3...v0.9.4)
  303. * [v0.9.3](https://github.com/felixge/node-formidable/compare/v0.9.2...v0.9.3)
  304. * [v0.9.2](https://github.com/felixge/node-formidable/compare/v0.9.1...v0.9.2)
  305. * [v0.9.1](https://github.com/felixge/node-formidable/compare/v0.9.0...v0.9.1)
  306. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  307. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  308. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  309. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  310. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  311. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  312. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  313. * [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
  314. * [v0.1.0](https://github.com/felixge/node-formidable/commits/v0.1.0)
  315. ## License
  316. Formidable is licensed under the MIT license.
  317. ## Ports
  318. * [multipart-parser](http://github.com/FooBarWidget/multipart-parser): a C++ parser based on formidable
  319. ## Credits
  320. * [Ryan Dahl](http://twitter.com/ryah) for his work on [http-parser](http://github.com/ry/http-parser) which heavily inspired multipart_parser.js