Vue觸發(fā)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; } }
補充知識: vue下打包時幾個文件選擇文件打包一起 并做懶加載
直接上代碼
const DeviceManage = r => require.ensure([], () => r(require(deviceManagePath + 'main/DeviceManage')), 'g-DeviceManage'); const SingleDeviceSet = r => require.ensure([], () => r(require(deviceManagePath + 'deviceSet/SingleDeviceSet')), 'g-DeviceManage'); const ModifyNo = r => require.ensure([], () => r(require(deviceManagePath + 'modifyNo/ModifyNo')), 'g-DeviceManage'); const PricePerTime = r => require.ensure([], () => r(require(deviceManagePath + 'pricePerTime/PricePerTime')), 'g-DeviceManage'); const SetParams = r => require.ensure([], () => r(require(deviceManagePath + 'setParams/SetParams')), 'g-DeviceManage'); const ShowDevicePrice = r => require.ensure([], () => r(require(deviceManagePath + 'showDevicePrice/ShowDevicePrice')), 'g-DeviceManage'); const parameterSetting = r => require.ensure([], () => r(require(deviceManagePath + 'parameterSetting/parameterSetting')), 'g-DeviceManage'); const SetParams3G = r => require.ensure([], () => r(require(deviceManagePath + 'setParams3G/SetParams3G')), 'g-Device3Gparams');
這么寫 所有g-DeviceManage的文件會被打包在一起
以上這篇Vue觸發(fā)input選取文件點擊事件操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue使用mpegts.js實現(xiàn)播放flv的直播視頻流
這篇文章主要為大家詳細介紹了vue如何使用mpegts.js實現(xiàn)播放flv的直播視頻流,文中的示例代碼講解詳細,有需要的小伙伴可以參考一下2024-01-01vue通過vue-lazyload實現(xiàn)圖片懶加載的代碼詳解
這篇文章主要給大家介紹了vue通過vue-lazyload實現(xiàn)圖片懶加載,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-02-02Vue時間軸 vue-light-timeline的用法說明
這篇文章主要介紹了Vue時間軸 vue-light-timeline的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10ant design vue動態(tài)循環(huán)生成表單以及自定義校驗規(guī)則詳解
這篇文章主要介紹了ant design vue動態(tài)循環(huán)生成表單以及自定義校驗規(guī)則詳解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01vue項目實現(xiàn)路由跳轉到新頁面,返回舊頁面,保留之前的數(shù)據(jù)記錄(操作代碼)
這篇文章主要介紹了vue項目實現(xiàn)路由跳轉到新頁面,返回舊頁面,保留之前的數(shù)據(jù)記錄,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09解決axios:"timeout of 5000ms exceeded"
這篇文章主要介紹了解決axios:"timeout of 5000ms exceeded"超時的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08