Vue觸發input選取文件點擊事件


CSS

 

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

 

 

 

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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM