# 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
没有合适的资源?快使用搜索试试~ 我知道了~
ASP.NET MVC+EF搭建面向接口框架
共1023个文件
js:250个
dll:144个
cs:121个
需积分: 40 40 下载量 49 浏览量
2018-04-07
12:20:54
上传
评论
收藏 30.29MB RAR 举报
温馨提示
搭建系统MVC5+EF6搭建框架EF Code frist、实现泛型数据仓储以及业务逻辑等等
资源推荐
资源详情
资源评论
收起资源包目录
ASP.NET MVC+EF搭建面向接口框架 (1023个子文件)
Global.asax 109B
Wchl.WMBlog.WebUI.csprojResolveAssemblyReference.cache 263KB
Wchl.WMBlog.WebCore.csprojResolveAssemblyReference.cache 107KB
DesignTimeResolveAssemblyReferencesInput.cache 56KB
Wchl.WMBlog.Services.csprojResolveAssemblyReference.cache 53KB
Wchl.WMBlog.Repository.csprojResolveAssemblyReference.cache 25KB
Wchl.WMBlog.Common.csprojResolveAssemblyReference.cache 24KB
Wchl.WMBlog.Model.csprojResolveAssemblyReference.cache 19KB
Wchl.WMBlog.IServices.csprojResolveAssemblyReference.cache 15KB
Wchl.WMBlog.IRepository.csprojResolveAssemblyReference.cache 15KB
DesignTimeResolveAssemblyReferencesInput.cache 8KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
ApplicationInsights.config 6KB
ApplicationInsights.config 6KB
Wchl.WMBlog.WebUI.dll.config 6KB
Web.config 6KB
packages.config 5KB
NLog.config 2KB
NLog.config 2KB
Web.config 2KB
web.config 2KB
NLog.config 1KB
NLog.config 1KB
NLog.config 1KB
NLog.config 1KB
Web.Release.config 1KB
Web.Debug.config 1KB
Wchl.WMBlog.Model.dll.config 1KB
App.config 1KB
Wchl.WMBlog.Repository.dll.config 790B
App.config 790B
packages.config 631B
VBCSCompiler.exe.config 563B
Wchl.WMBlog.WebCore.dll.config 429B
app.config 429B
packages.config 365B
packages.config 228B
packages.config 143B
packages.config 138B
AccountController.cs 17KB
ManageController.cs 14KB
BaseRepository.cs 8KB
BlogController.cs 6KB
Tools.cs 5KB
BaseServices.cs 5KB
AdvertisementController.cs 5KB
CheckLoginAttribute.cs 4KB
IdentityConfig.cs 4KB
BlogArticleController.cs 4KB
IBaseRepository.cs 4KB
IBaseServices.cs 4KB
RedisCacheManager.cs 3KB
LoginController.cs 3KB
Startup.Auth.cs 3KB
AccountViewModels.cs 3KB
Module.cs 3KB
HomeController.cs 3KB
ManageViewModels.cs 3KB
Permission.cs 2KB
AutofacConfig.cs 2KB
BlogArticleServices.cs 2KB
BlogViewModels.cs 2KB
UserInfoController.cs 2KB
Role.cs 2KB
BaseController.cs 2KB
VcodeController.cs 2KB
sysUserInfo.cs 2KB
MemoryCacheManager.cs 2KB
RoleModulePermission.cs 2KB
WMBlogDB.cs 2KB
ExpFilter.cs 2KB
ModulePermission.cs 1KB
UserRole.cs 1KB
NLogLogger.cs 1KB
OperateLog.cs 1KB
BlogArticle.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
AssemblyInfo.cs 1KB
GuestbookViewModels.cs 1KB
JsonNetResult.cs 1KB
IdentityModels.cs 1KB
GuestbookServices.cs 1KB
BundleConfig.cs 1KB
Guestbook.cs 1KB
ModuleMap.cs 1KB
RoleModulePermissionMap.cs 1KB
SerializeHelper.cs 1KB
Keys.cs 1KB
Configuration.cs 1KB
共 1023 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
寒冷记忆
- 粉丝: 3
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功