图片上传

this.$nextTick(function() {
let _self = this;
$(this.$el).on('change', 'input[type="file"]', function() {
// 判断图片类型
const rFilter = /^(?:image\/jpeg|image\/png|image\/bmp)$/i;
const imgFile = this.files[0];
if(!imgFile) return;
const _this = this;
const reader = new FileReader();
reader.onload = function(e){
const data = e.target.result;
const image = new Image();
image.src = data;
image.onload = function(){
const width = image.width;
const height = image.height;
if(width != 1029 || height != 270){
$(_this).val('');
return globalBus.$emit('warning', '尺寸需要1029*270哦!');
}else{
if(rFilter.test(imgFile.type)){
// 判断图片大小,通过max-size传值,100kb
if(Math.ceil(imgFile.size / 1024) < _self.maxSize){
common.ajax({
target: $(_self.$el),
url: DI.common.upload,
success: function(data){
if(data.retCode == 0){
const info = data.data;
console.log(info);
_self.$emit('input', info)
}else{
globalBus.$emit('warning', data.retMsg);
}
}
}, true)
}else{
$(_this).val('');
globalBus.$emit('warning', '图片大小不能超过' + _self.maxSize + 'Kb');

}
}else{
$(_this).val('');
globalBus.$emit('warning', '商品图片格式需要上传jpg,png,bmp哦');

}
}
}

}
reader.readAsDataURL(imgFile);

});
});
posted @ 2017-08-21 11:17  Peter.seven  阅读(149)  评论(0编辑  收藏  举报