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' @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觸發input選取文件點擊事件操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. Docker究竟是什么 為什么這么流行 它的優點和缺陷有哪些?2. idea打開多個窗口的操作方法3. IntelliJ IDEA設置編碼格式的方法4. IDEA 重新導入依賴maven 命令 reimport的方法5. 如何通過vscode運行調試javascript代碼6. Java14發布了,再也不怕NullPointerException了7. IntelliJ IDEA 2020.2正式發布,兩點多多總能助你提效8. Intellij IDEA 閱讀源碼的 4 個絕技(必看)9. IntelliJ IDEA 統一設置編碼為utf-8編碼的實現10. IntelliJ IDEA設置背景圖片的方法步驟
