手动上传图片不需要action,如果需要给事件传参就需要以下的方式,第三个参数后是自己要传的参数
<el-upload
class="centerImg"
action=""
list-type="picture-card"
:auto-upload="false"
:on-change="
(file, fileList) => {
handleAvatarChangeIcon(file, fileList, 'file');
}
"
:on-exceed="imgExceed"
:on-remove="handREmove"
ref="uploadicon"
>
<i class="el-icon-plus"></i>
</el-upload>
handleAvatarChangeIcon(file, fileList, logo) {
const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
const whiteList = ["jpg", "png", "gif"];
if (whiteList.indexOf(fileSuffix) === -1) {
this.$message({
message: "上传文件只能是 jpg、png、gif格式",
type: "warning",
});
return false;
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
this.$message({
message: "上传文件大小不能超过 2MB",
type: "warning",
});
return false;
}
this.iconformData = {img:file.raw}
}
saveIco() {
let config = {
headers: {
"Content-Type": "multipart/form-data",
}
};
let from = {
updateFrom: this.updateFrom,
goodsValue: this.goodsValue,
}
const formData = new FormData();
formData.append("logo", this.iconformData.img);
formData.append("from", JSON.stringify(from));
this.$http
.post(
"",
formData,
config
)
.then((res) => {
})
.catch((error) => {
});
},
后端node接收
let express = require('express');
let router = express.Router();
let path = require("path");
const multer = require('multer');
const fs = require('fs');
let stroage = multer.diskStorage({
destination: async function (req, file, cd) {
let pat = `./public/images`
if (!fs.existsSync(pat)) {
let dome = await fs.mkdir(
path.resolve(pat),
{recursive: true},
async (err) => {
if (err) { return console.error(err); }
return cd(null, pat)
}
);
} else {
cd(null, `./public/images`)
}
},
filename: function (req, file, cb) {
cb(null, `${file.originalname}`)
},
})
let upload = multer({
storage: stroage,
limits: 1024 * 1024 * 3
});
router.post('/', upload.any(), async (req, res, next) => {
try {
let form = req.files[0]
let url = ``
return res.json({
code: 200,
message: '上传成功',
url
})
} catch (error) {
console.log(error);
}
})