multipart-data
Simple multipart/form-data parsing or building module
Usage
npm i multipart-data -S
const http = const Form = Form http
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
<Incoming>callback
<Function>err
<Error>fields
<Object>text
字段数据files
<Object> 文件数据
当提供
callback
时,数据会自动被处理,文件会自动保存到uploadDir
中。当全部数据处理完毕时,会调用callback
,当出现错误时也一样。
const http = const Form = Form http/**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
file
解析一个文件时解发。
file
<Object>name
<string> 字段名filename
<string> 文件名size
<number> 文件体积。单位:byte
type
<string> 文件类型。示例:image/jpeg
path
<string> 文件保存的绝对路径
form
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 = const http = http