0.0.1 • Published 8 years ago
multipart-data v0.0.1
multipart-data
Simple multipart/form-data parsing or building module
Usage
npm i multipart-data -Sconst http = require('http')
const Form = require('multipart-data').Form
http.createServer(function (req, res) {
if (req.method.toLowerCase() === 'post' && req.url === '/upload') {
const form = new Form()
form.parse(req, function (err, fields, files) {
if (!err) {
console.log(fields)
console.log(files)
res.end()
}
})
} else {
res.end()
}
})new Form(options)
options\<Object>maxFieldsSize\<number> 当text类型的内容数据超出时触发error事件。单位:byte,默认2MB。maxFields\<number> 当text类型的字段数量超出时触发error事件。默认1000。maxFilesSize\<number> 当file类型的内容数据超出时触发error事件。单位:byte。默认Infinity。uploadDir\<string> 文件上传保存目录。默认os.tmpdir()。
Function
parse(req, callback)
解析请求对象。
req\callback\<Function>err\<Error>fields\<Object>text字段数据files\<Object> 文件数据
当提供
callback时,数据会自动被处理,文件会自动保存到uploadDir中。当全部数据处理完毕时,会调用callback,当出现错误时也一样。
const http = require('http')
const Form = require('multipart-data').Form
http.createServer(function (req, res) {
if (req.method.toLowerCase() === 'post' && req.url === '/upload') {
const form = new Form()
form.parse(req, function (err, fields, files) {
if (!err) {
console.log(fields)
console.log(files)
res.end()
}
})
} else {
res.end()
}
})
/**
print files
{
"file": {
"filename": "1.jpg",
"size": 326603,
"type": "image/jpeg",
"path": "/var/folders/vs/kf0w144556g2gb4fr4j3v2xh0000gn/T/15122353640973214"
},
"file2": {
"filename": "2.jpeg",
"size": 98817,
"type": "image/jpeg",
"path": "/var/folders/vs/kf0w144556g2gb4fr4j3v2xh0000gn/T/15122353641153904"
}
}
print fields
{
"name": "xiaobai"
}
*/Event
error
错误事件
err\<Error>
close
数据处理完成事件。
field
解析一个 text 字段时触发
field\<Object>name\<string> 字段名value\<string> 字段值
form.on('field', function (field) {
console.log(field)
/**
{ name: 'age',
value: '12' }
*/
})file
解析一个文件时解发。
file\<Object>name\<string> 字段名filename\<string> 文件名size\<number> 文件体积。单位:bytetype\<string> 文件类型。示例:image/jpegpath\<string> 文件保存的绝对路径
form.on('file', function (file) {
console.log(file)
/**
{ filename: '1.jpg',
size: 326603,
type: 'image/jpeg',
path: '/var/folders/vs/kf0w144556g2gb4fr4j3v2xh0000gn/T/15122352606768469',
name: 'file' }
*/
})part
当解析数据一部分时触发。
part\<Readable> 当前部分的可读流。同时具备下列属性。isFile\<boolean> 是否文件name\<string> 字段名filename\<string> 文件名,仅当isFile为true时。type\<string> 文件类型,仅当isFile为true时。
当需要获取数据流时可以绑定
part事件,流开始时是暂停状态,要绑定data事件或调用resume()方法或pipe()方法改变为流动状态。当调用
parse()方法时提供callback或绑定了field事件和file事件时,数据会自动被处理,因此part事件不会触发。
const Form = require('multipart-data')
const http = require('http')
http.createServer(function (req, res) {
if (req.method.toLowerCase() === 'post' && req.url === '/upload') {
const form = new Form()
form.on('part', function (part) {
part.pipe(require('fs').createWriteStream(__dirname + '/' + part.filename))
})
form.on('field', function (field) {
console.log(field)
})
form.on('close', function () {
console.log('ok')
res.end('ok')
})
form.parse(req)
} else {
res.end()
}
}).listen(8000)0.0.1
8 years ago