# Formidable
[![Build Status](https://secure.travis-ci.org/felixge/node-formidable.png?branch=master)](http://travis-ci.org/felixge/node-formidable)
## Purpose
A node.js module for parsing form data, especially file uploads.
## Current status
This module was developed for [Transloadit](http://transloadit.com/), a service focused on uploading
and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from
a large variety of clients and is considered production-ready.
## Features
* Fast (~500mb/sec), non-buffering multipart parser
* Automatically writing file uploads to disk
* Low memory footprint
* Graceful error handling
* Very high test coverage
## Installation
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.
Via [npm](http://github.com/isaacs/npm):
```
npm install formidable@latest
```
Manually:
```
git clone git://github.com/felixge/node-formidable.git formidable
vim my.js
# var formidable = require('./formidable');
```
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.
## Example
Parse an incoming file upload.
```javascript
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8080);
```
## API
### Formidable.IncomingForm
```javascript
var form = new formidable.IncomingForm()
```
Creates a new incoming form.
```javascript
form.encoding = 'utf-8';
```
Sets encoding for incoming form fields.
```javascript
form.uploadDir = "/my/dir";
```
Sets the directory for placing file uploads in. You can move them later on using
`fs.rename()`. The default is `os.tmpDir()`.
```javascript
form.keepExtensions = false;
```
If you want the files written to `form.uploadDir` to include the extensions of the original files, set this property to `true`.
```javascript
form.type
```
Either 'multipart' or 'urlencoded' depending on the incoming request.
```javascript
form.maxFieldsSize = 2 * 1024 * 1024;
```
Limits the amount of memory all fields together (except files) can allocate in bytes.
If this value is exceeded, an `'error'` event is emitted. The default
size is 2MB.
```javascript
form.maxFields = 1000;
```
Limits the number of fields that the querystring parser will decode. Defaults
to 1000 (0 for unlimited).
```javascript
form.hash = false;
```
If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.
```javascript
form.multiples = false;
```
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.
```javascript
form.bytesReceived
```
The amount of bytes received for this form so far.
```javascript
form.bytesExpected
```
The expected number of bytes in this form.
```javascript
form.parse(request, [cb]);
```
Parses an incoming node.js `request` containing form data. If `cb` is provided, all fields and files are collected and passed to the callback:
```javascript
form.parse(req, function(err, fields, files) {
// ...
});
form.onPart(part);
```
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.
```javascript
form.onPart = function(part) {
part.addListener('data', function() {
// ...
});
}
```
If you want to use formidable to only handle certain parts for you, you can do so:
```javascript
form.onPart = function(part) {
if (!part.filename) {
// let formidable handle all non-file parts
form.handlePart(part);
}
}
```
Check the code in this method for further inspiration.
### Formidable.File
```javascript
file.size = 0
```
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.
```javascript
file.path = null
```
The path this file is being written to. You can modify this in the `'fileBegin'` event in
case you are unhappy with the way formidable generates a temporary path for your files.
```javascript
file.name = null
```
The name this file had according to the uploading client.
```javascript
file.type = null
```
The mime type of this file, according to the uploading client.
```javascript
file.lastModifiedDate = null
```
A date object (or `null`) containing the time this file was last written to. Mostly
here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
```javascript
file.hash = null
```
If hash calculation was set, you can read the hex digest out of this var.
#### Formidable.File#toJSON()
This method returns a JSON-representation of the file, allowing you to
`JSON.stringify()` the file which is useful for logging and responding
to requests.
### Events
#### 'progress'
```javascript
form.on('progress', function(bytesReceived, bytesExpected) {
});
```
Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
#### 'field'
```javascript
form.on('field', function(name, value) {
});
```
#### 'fileBegin'
Emitted whenever a field / value pair has been received.
```javascript
form.on('fileBegin', function(name, file) {
});
```
#### 'file'
Emitted whenever a new file is detected in the upload stream. Use this even if
you want to stream the file to somewhere else while buffering the upload on
the file system.
Emitted whenever a field / file pair has been received. `file` is an instance of `File`.
```javascript
form.on('file', function(name, file) {
});
```
#### 'error'
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.
```javascript
form.on('error', function(err) {
});
```
#### 'aborted'
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).
```javascript
form.on('aborted', function() {
});
```
##### 'end'
```javascript
form.on('end', function() {
});
```
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.
## Changelog
### v1.0.14
* Add failing hash tests. (Ben Trask)
* Enable hash calculation again (Eugene Girshov)
* Test for immediate data events (Tim Smart)
* Re-arrange IncomingForm#parse (Tim Smart)
### v1.0.13
* Only update hash if update method exists (Sven Lito)
* According to travis v0.10 needs to go quoted (Sven Lito)
* Bumping build node versions
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 基于Python+Flask框架的博客管理系统设计与实现-毕业设计源码+使用文档(高分优秀项目).zip基于Python+Flask框架的博客管理系统设计与实现-毕业设计源码+使用文档(高分优秀项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕设项目、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 3、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于Python+Flask框架的博客管理系统设计与实现-毕业设计源码+使用文档(高分优秀项目).zip (708个子文件)
bootstrap.css 144KB
bootstrap.min.css 120KB
layui.css 59KB
animate.min.css 55KB
jquery.mCustomScrollbar.css 52KB
ueditor.css 43KB
font-awesome.css 37KB
ueditor.min.css 34KB
font-awesome.min.css 30KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
wangEditor.css 18KB
wangEditor.css 18KB
image.css 18KB
style.css 15KB
wangEditor.min.css 15KB
video.css 15KB
layer.css 14KB
attachment.css 14KB
blog.css 14KB
index_style.css 13KB
layui.mobile.css 10KB
laydate.css 7KB
master.css 5KB
style.css 5KB
gloable.css 4KB
scrawl.css 4KB
charts.css 3KB
background.css 2KB
about.css 2KB
demo.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
dark.css 2KB
github.css 2KB
message.css 1KB
nprogress.css 1KB
edittable.css 1KB
code.css 1KB
template.css 1KB
help.css 389B
iframe.css 41B
emotions.data 29KB
fontawesome-webfont.eot 162KB
barbarahand-webfont.eot 59KB
iconfont.eot 40KB
glyphicons-halflings-regular.eot 20KB
icomoon.eot 15KB
icomoon.eot 15KB
adminthemes-webfont.eot 6KB
lg.eot 4KB
5.gif 367KB
2.gif 367KB
6.gif 296KB
1.gif 288KB
3.gif 247KB
4.gif 247KB
wface.gif 49KB
jxface2.gif 40KB
yface.gif 28KB
bface.gif 27KB
icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
tface.gif 19KB
fface.gif 18KB
59.gif 10KB
22.gif 10KB
cface.gif 8KB
17.gif 8KB
18.gif 8KB
24.gif 8KB
11.gif 8KB
13.gif 7KB
30.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
63.gif 6KB
50.gif 6KB
loading-0.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
21.gif 5KB
20.gif 5KB
8.gif 5KB
28.gif 5KB
29.gif 5KB
70.gif 4KB
5.gif 4KB
42.gif 4KB
31.gif 4KB
17.gif 4KB
27.gif 4KB
9.gif 4KB
1.gif 4KB
44.gif 4KB
共 708 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
不走小道
- 粉丝: 3225
- 资源: 5113
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功