Accept a single file with the name fieldname. The single file will be stored in req.file.
Accept an array of files, stored in req.files
Accept a mix of files, stored in req.files
[ { name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 } ]
Accept only text fields
Accepts all files
分类 | 说明 |
---|---|
API接口 | /upload |
name属性 | image |
npm install multer --save
const multer = require('multer')
const path = require('path')
Multer accepts an options object, the most basic of which is the dest property, which tells Multer where to upload the files. In case you omit the options object, the files will be kept in memory and never written to disk.
In an average web app, only dest might be required.
more control over your uploads, use the storage option instead of dest.
使用 diskStorage 配置可以更好控制上传细节,如改名;有了diskStorage,可以放弃 dest 配置
使用时间戳为上传图片拼接名字
文件的存储位置和命名方式,根据实际情况定制,如可以按照学号+姓名命名头像文件
const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, __dirname + '/public/upload') }, filename: (req, file, cb) => { let ext = path.extname(file.originalname) cb(null, file.fieldname + Date.now() + ext) } })
const upload = multer({ storage: storage })
req.file可以获取到具体的文件信息
app.post('/upload', upload.single('image'), (req, res) => { res.send({ err: 0, url: 'http://127.0.0.1:3000/upload/' + req.file.filename }) })
默认情况下,只是简单的将文件上传到指定目录;不会解析文件信息,如文件名或文件后缀
{ fieldname: 'image', originalname: 'XG7mQxuM7nYJ787bce43d31211e2a9fb482c28888418.png', encoding: '7bit', mimetype: 'image/png', destination: 'upload', filename: '7d9d07bdadd775e054e0e0e00fe2d444', path: 'upload\\7d9d07bdadd775e054e0e0e00fe2d444', size: 91117 }
const express = require('express') const multer = require('multer') const path = require('path') const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, 'public/upload') }, filename: (req, file, cb) => { let ext = path.extname(file.originalname) cb(null, file.fieldname + Date.now() + ext) } }) const upload = multer({ storage: storage }) const port = 3000 const app = express() app.use(express.static('./public')); app.post('/upload', upload.single('image'), (req, res) => { console.log(req.file) //res.send(req.file) res.send({ err: 0, url: 'http://127.0.0.1:3000/upload/' + req.file.filename }) }) app.listen(port, () => { console.log('server now is at 127.0.0.1:3000'); })
分类 | 说明 |
---|---|
API接口 | /upload/avatar |
图片 | avatar |
学号 | usn |
姓名 | uname |
<form action="http://127.0.0.1:3000/upload/avatar" enctype="multipart/form-data" method="post"> <input type="file" name="avatar"> <input type="text" placeholder="your sn" name="usn"> <input type="text" placeholder="your name" name="uname"> <button>submit</button> </form>
router.post('/mix', upload.single('uimg'), (req, res) => { res.send({ msg: 'ok' }) })
const multer = require('multer') const fs = require('fs'); const path = require('path') const sd = require('silly-datetime'); // Set up storage for uploaded files const storage = multer.diskStorage({ destination: (req, file, callback) => { let date = sd.format(new Date(), 'YYYYMMDD'); let dir = path.join('public/upload', date) if (!fs.existsSync(dir)) { fs.mkdirSync(dir) } callback(null, dir); }, filename: (req, file, callback) => { let ext = path.extname(file.originalname) callback(null, file.fieldname + Date.now() + ext); } }); // Create the multer instance const upload = multer({ storage: storage }); module.exports = upload
分类 | 说明 |
---|---|
API接口 | /upload/text |
名字 | uname |
密码 | upass |
<form action="http://127.0.0.1:3000/upload/text" enctype="multipart/form-data" method="post"> <input type="text" placeholder="your name" name="uname"> <input type="password" placeholder="your pass" name="upass"> <button>submit</button> </form>
router.post('/text', upload.none(), (req, res) => { res.send({ msg: 'ok' }) })
<input type="file" name="img" multiple>
router.post('/array', upload.array('img', 9), (req, res) => { res.send({ err: 0, msg: 'ok', data: req.body, file: req.files }) })