Vue触发input点击事件

HTML

<div class="upload-btn-box">
  <Button @click="choiceImg" icon="ios-cloud-upload-outline" type="primary">点击上传</Button>
    <input ref="filElem" type="file" class="upload-file" @change="getFile">
</div>

Script

choiceImg(){
    this.$refs.filElem.dispatchEvent(new MouseEvent('click')) 
},
getFile(){
            var that = this;
            const inputFile = this.$refs.filElem.files[0];
            if(inputFile){
                if(inputFile.type !== 'image/jpeg' && inputFile.type !== 'image/png' && inputFile.type !== 'image/gif'){
                    alert('不是有效的图片文件!');
                    return;
                }
                this.imgInfo = Object.assign({}, this.imgInfo, {
                    name: inputFile.name,
                    size: inputFile.size,
                    lastModifiedDate: inputFile.lastModifiedDate.toLocaleString()
                })
                const reader = new FileReader();
                reader.readAsDataURL(inputFile);
                reader.onload = function (e) {
                    that.imgSrc = this.result;
                }
            } else {
                return;
            }
        }

 

猜你喜欢

转载自www.cnblogs.com/hzx-5/p/9957726.html
今日推荐