本文用node进行图片上传主要借助formidable插件,具体使用步骤如下:
1、安装formidable插件
npm install formidable -D
2、引入依赖包
const formidable = require('formidable');
3、接口实现逻辑
router.post('/uploadFile', (req, res, next) => {
let form = new formidable.IncomingForm();
form.encoding = 'utf-8';
//上传到服务器指定文件夹
form.uploadDir =`public/${constant.UPLOAD_FOLDER}`;
form.keepExtensions = true; //保留后缀
form.maxFieldsSize = constant.IMAGE_SIZE;
let data = new MsgBean('上传失败',1);
form.parse(req, (err, fields, files) => {
if (err) {
data.setContent(err);
res.send(data);
return;
}
let file = files.file;
let fileType = new RegExp(file.type, 'g'); let isLegal = fileType.test(constant.IMAGE_TYPE);
if(!isLegal) {//判断上传图片格式是否合法
data.msg('非法的图片格式');
res.send(data);
return;
}
let fileSize = file.size;
if(fileSize > constant.IMAGE_SIZE) {//上传图片超出最大限度
data.msg('图片大小超出最大限制');
res.send(data);
return;
}
let index = file.name.lastIndexOf('.');
let oldPath = files.file.path;
let uploadDate = new Date().getTime();
let fileName = `${file.name.slice(0, index)}_${uploadDate}_origin.${file.name.slice(index+1)}`;
let newPath = `public/${constant.UPLOAD_FOLDER}/${fileName}`;
fs.rename(oldPath, newPath, err => {//图片更名
if(err) {
data.setContent('图片解析失败');
res.send(data);
return;
}
let baseUrl = req.headers.host;
let imgUrl = `http://${baseUrl}/${constant.UPLOAD_FOLDER}/${fileName}`;
let result = {
imgUrl
};
data.setCode(0);
data.setContent(result);
data.setMsg('上传成功');
res.send(data);
});
})
});