# 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
没有合适的资源?快使用搜索试试~ 我知道了~
【java毕业设计】校园博客项目源码源码(springboot+vue+mysql+说明文档).zip
共540个文件
js:157个
java:85个
html:73个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 122 浏览量
2024-11-13
08:31:34
上传
评论
收藏 5.1MB ZIP 举报
温馨提示
环境说明: 开发语言:Java 框架:springboot,mybatis JDK版本:JDK1.8 数据库:mysql 5.7数据库工具:Navicat11开发软件:eclipse/idea Maven包:Maven3.3
资源推荐
资源详情
资源评论
收起资源包目录
【java毕业设计】校园博客项目源码源码(springboot+vue+mysql+说明文档).zip (540个子文件)
bootstrap.css 144KB
bootstrap.css 134KB
bootstrap.min.css 120KB
bootstrap.min.css 111KB
font-awesome-ie7.min.css 37KB
font-awesome.css 37KB
font-awesome.css 34KB
font-awesome.min.css 30KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
font-awesome.min.css 22KB
bootstrap-theme.css 21KB
bootstrap-theme.min.css 19KB
wangEditor.css 18KB
wangEditor.css 18KB
wangEditor.min.css 15KB
style.css 5KB
jquery-labelauty.css 3KB
demo.css 2KB
dark.css 2KB
github.css 2KB
login.css 2KB
web-model.css 1KB
style.css 1KB
main.css 1KB
web-page.css 592B
nav.css 509B
form-page.css 435B
list-page.css 304B
main.css 170B
main.css 103B
register.css 69B
emotions.data 29KB
Dockerfile 166B
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 75KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
icomoon.eot 15KB
icomoon.eot 15KB
5.gif 367KB
2.gif 367KB
6.gif 296KB
1.gif 288KB
4.gif 247KB
3.gif 247KB
17.gif 8KB
18.gif 8KB
11.gif 8KB
30.gif 7KB
8.gif 5KB
28.gif 5KB
42.gif 4KB
31.gif 4KB
1.gif 4KB
25.gif 4KB
7.gif 4KB
10.gif 4KB
46.gif 4KB
9.gif 3KB
27.gif 3KB
6.gif 3KB
21.gif 3KB
48.gif 3KB
26.gif 3KB
38.gif 3KB
47.gif 2KB
24.gif 2KB
23.gif 2KB
12.gif 2KB
32.gif 2KB
5.gif 2KB
22.gif 2KB
20.gif 2KB
4.gif 2KB
14.gif 2KB
19.gif 2KB
3.gif 2KB
36.gif 2KB
29.gif 2KB
13.gif 2KB
35.gif 2KB
2.gif 2KB
41.gif 2KB
15.gif 2KB
33.gif 1KB
16.gif 1KB
39.gif 1KB
40.gif 1KB
34.gif 1KB
44.gif 1KB
49.gif 1KB
37.gif 1KB
43.gif 1KB
45.gif 1KB
50.gif 1017B
.gitattributes 483B
.gitignore 674B
.gitignore 380B
demo.html 53KB
共 540 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
风月歌
- 粉丝: 1647
- 资源: 4247
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功