// 需要引入的包
const multer = require(‘multer’)
const fs = require(‘fs’)
直接粘贴复制使用就行
//图片上传
router.post(
"/api/hold/imgload",
multer({
//设置文件存储路径
dest: "public/images",
}).array("file", 1),
function (req, res, next) {
let files = req.files;
let file = files[0];
let fileInfo = {};
let path = "public/images/" + Date.now().toString() + "_" + file.originalname;
fs.renameSync("./public/images/" + file.filename, path);
//获取文件基本信息
fileInfo.type = file.mimetype;
fileInfo.name = file.originalname;
fileInfo.size = file.size;
fileInfo.path = path;
res.send({
code: 200,
msg: "OK",
data: fileInfo,
});
}
)