Vue input 선택 파일 클릭 이벤트 트리거

4342 단어
CSS
 
.upload-btn-box {
    margin-bottom: 10px;
    button {
        margin-right: 10px;
    }
    input[type=file] {
        display: none;
    }
}

 
 
 
HTML
class="upload-btn-box">    ref="filElem" type="file" class="upload-file" @change="getFile">

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;
            }
        }

 
 
전재 대상:https://www.cnblogs.com/hzx-5/p/9957726.html

좋은 웹페이지 즐겨찾기