key | description |
---|---|
dest or storage | Where to store the files |
fileFilter | Function to control which files are accepted |
limits | Limits of the uploaded data |
Accept a single file with the name fieldname. The single file will be stored in req.file.
接收一个叫做
Accept an array of files, all with the name fieldname. Optionally error out if more than maxCountfiles are uploaded. The array of files will be stored in req.files.
接收一个所有附件的名字是
Accept a mix of files, specified by fields. An object with arrays of files will be stored inreq.files.
fields should be an array of objects with name and optionally a maxCount. Example:
接收所有名称的附件,附件将被保存到req.files属性中(是一个对象数组),配置参数如下
Accepts all files that comes over the wire. An array of files will be stored in req.files.
接收所有提交的数据,保存到req.files属性中
The disk storage engine gives you full control on storing files to disk.
你可以使用硬盘存储模式设置在哪里存放上传的附件
Set this to a function to control which files should be uploaded and which should be skipped. The function should look like this:
设置什么种类的文件可以上传,什么种类的文件被跳过,例如
When encountering an error, multer will delegate the error to express. You can display a nice error page using the standard express way.
If you want to catch errors specifically from multer, you can call the middleware function by yourself.
当上传过程出现错误,multer将把错误信息发送给express,你可以做一个显示错误的界面。如果你希望从multer获取错误,你可以调用你自己写的中间件
npm init
//输入package.json 的相关信息后会自动生成package.json
//加入express 和 multer模块
npm install express --save
npm install multer --save
{ fieldname: 'avatar',
originalname: '2222222.png',
encoding: '7bit',
mimetype: 'image/png',
destination: '/home/nodejs/multertest/temp',
filename: '9e40b3515891112ebf69b6a1547a83ba',
path: '/home/nodejs/multertest/temp/9e40b3515891112ebf69b6a1547a83ba',
size: 7864 }
{ aaaa: 'aaaa', bbb: 'bbbb' }
[ { fieldname: 'photos',
originalname: '2222222.png',
encoding: '7bit',
mimetype: 'image/png',
destination: '/home/nodejs/multertest/temp',
filename: 'e21933dc78324e78274d9f69dece94ba',
path: '/home/nodejs/multertest/temp/e21933dc78324e78274d9f69dece94ba',
size: 7864 },
{ fieldname: 'photos',
originalname: 'QQ妾?0151209185325.png',
encoding: '7bit',
mimetype: 'image/png',
destination: '/home/nodejs/multertest/temp',
filename: '4109fccc094d0f1429d80f16dc53dfa3',
path: '/home/nodejs/multertest/temp/4109fccc094d0f1429d80f16dc53dfa3',
size: 2192 },
{ fieldname: 'photos',
originalname: 'signupSubmit.js',
encoding: '7bit',
mimetype: 'application/x-js',
destination: '/home/nodejs/multertest/temp',
filename: 'bcc2e2bf9e9a7cc12eab997c99fec044',
path: '/home/nodejs/multertest/temp/bcc2e2bf9e9a7cc12eab997c99fec044',
size: 1868 } ]
{ aaaa: 'ssssss', bbb: 'ddddd' }
文件属性说明:
属性名 | 描述 | 备注 |
---|---|---|
fieldname | Field name specified in the form | |
originalname | Name of the file on the user's computer | |
encoding | Encoding type of the file | |
mimetype | Mime type of the file | |
size | Size of the file in bytes | |
destination | The folder to which the file has been saved | DiskStorage |
filename | The name of the file within the destination | DiskStorage |
path | The full path to the uploaded file | DiskStorage |
buffer | A Buffer of the entire file | MemoryStorage |