vue上传图片

// 上传限制
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg';
const isPNG = file.type === 'image/png';
const isLt2M = file.size / 1024 / 1024 < 5;
var isIMG = true;
if (!isJPG) {
if (!isPNG) {
this.$message.error('上传图片只能是 JPG, PNG 格式!');
isIMG = false;
}
}
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 5MB!');
}
return isIMG && isLt2M;
},
//商品图片上传(修改)
handleImgUpload: function(_index, _name, ev) {
var ev = event || window.event,
elem = ev.currentTarget,
imgElem = elem.nextElementSibling,
files = this.uploadImgFn(elem);
this.beforeAvatarUpload(files.fileObj);
imgElem.src = files.fileUrl;
if(_name == 'goods') { //商品图片
this.imgList[_index].fileUrl = files.fileUrl;
} else { //详情图片
this.remarkImgList[_index] = files.fileUrl;
}
this.uploadImg(files.fileObj, _index, _name);
},
//商品图片上传(删除)
handleImgDel: function(_index, _name, ev) {
var ev = event || window.event,
elem = ev.currentTarget,
imgElem = elem.parentElement;
if(_name == 'goods') { //商品图片
this.imgList.splice(_index, 1);
} else { //详情图片
this.remarkImgList.splice(_index, 1);
}
},
//商品图片上传(添加)
addImgUpload: function(_name, ev) {
var ev = event || window.event,
elem = ev.currentTarget,
num = 0,
files = this.uploadImgFn(elem); //获取上传的图片文件
this.beforeAvatarUpload(files.fileObj);
if(_name == 'goods') { //商品图片
num = this.imgList.length;
if(this.imgList.length >= 10) {
this.$message.error('最多只能添加10张图片');
return;
}
var obj = { fileCode: '', fileUrl: files.fileUrl }
this.imgList.push(obj);
} else {//详情图片
num = this.remarkImgList.length;
this.remarkImgList.push(files.fileUrl);
}
this.uploadImg(files.fileObj, num, _name);
elem.value = "";
},
//上传图片接口
uploadImg: function(_files, _index, _name) {
var that = this,
reqUrl = '地址',
formData = new FormData();
formData.append("token", that.token);
formData.append("files", _files);
this.$http.post(reqUrl, { formData: formData }).then(function(res) {
if (!res) { return; }
if(_name == 'goods') {//商品图片
that.imgList[_index].fileUrl = res.richImgs[0].picUrl;
} else {//详情图片
that.remarkImgList[_index] = res.richImgs[0].picUrl;
}
});
},

转载于:https://www.cnblogs.com/chase-star/p/10033719.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值