# 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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 基于docker的安全培训系统源码+详细文档(课程设计).zip基于docker的安全培训系统源码+详细文档(课程设计).zip 【备注】 1、该项目是个人高分课程设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于docker的安全培训系统源码+详细文档(课程设计).zip (794个子文件)
AUTHORS 767B
.bowerrc 40B
materialize.css 151KB
materialize.css 151KB
bootstrap.css 144KB
bootstrap.css 143KB
bootstrap.min.css 120KB
bootstrap.min.css 118KB
materialize.min.css 112KB
font-awesome.css 34KB
xterm.css 32KB
font-awesome.min.css 28KB
bootstrap-theme.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
wangEditor.css 18KB
style.css 15KB
wangEditor.min.css 15KB
style.css 5KB
nouislider.css 5KB
demo.css 2KB
dark.css 2KB
github.css 2KB
style.css 838B
style.css 785B
info.css 755B
index.css 660B
xterm.css 643B
login.css 401B
style.css 262B
fullscreen.css 150B
.csslintrc 456B
emotions.data 29KB
Procfile.dev 15B
sql注入盲注训练一.doc 17.06MB
宽字节注入详解一.doc 6.58MB
宽字节注入详解二.doc 6.01MB
sql注入盲注训练二.doc 5.21MB
sql注入基础训练一.doc 5.05MB
水平越权基础训练.doc 3.31MB
垂直越权基础训练.doc 2.98MB
远程文件包含基础训练二.doc 2.16MB
文件上传基础训练一.doc 2.11MB
文件上传基础训练二.doc 2.09MB
远程文件包含基础训练一.doc 2.05MB
csrf基础训练二.doc 1.78MB
命令注入基础训练二.doc 1.55MB
命令注入基础训练一.doc 1.14MB
存储型xss基础训练一.doc 990KB
csrf基础训练一.doc 977KB
dom型xss基础训练.doc 923KB
存储型xss基础训练二.doc 797KB
本地文件包含基础训练二.doc 750KB
文件下载基础训练.doc 738KB
xxe漏洞学习.doc 716KB
反射型xss基础训练二.doc 657KB
反射型xss基础训练一.doc 601KB
文件删除操作基础训练.doc 568KB
本地文件包含基础训练一.doc 490KB
url重定向基础训练.doc 388KB
突变型xss基础训练.doc 135KB
sql注入基础训练二.doc 86KB
弱加密算法基础训练.doc 66KB
Dockerfile 84B
.editorconfig 210B
.editorconfig 198B
fontawesome-webfont.eot 75KB
Roboto-Thin.eot 21KB
Roboto-Thin.eot 21KB
Roboto-Medium.eot 21KB
Roboto-Medium.eot 21KB
Roboto-Regular.eot 21KB
Roboto-Regular.eot 21KB
Roboto-Bold.eot 20KB
Roboto-Bold.eot 20KB
Roboto-Light.eot 20KB
Roboto-Light.eot 20KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
icomoon.eot 15KB
icomoon.eot 15KB
.eslintrc 360B
Gemfile 127B
generate-authors 231B
show.gif 1.54MB
效果展示.gif 1.12MB
效果图展示图.gif 843KB
5.gif 367KB
2.gif 367KB
6.gif 296KB
1.gif 288KB
3.gif 247KB
4.gif 247KB
效果展示1.gif 203KB
17.gif 8KB
18.gif 8KB
11.gif 8KB
30.gif 7KB
共 794 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
不走小道
- 粉丝: 3196
- 资源: 4313
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功