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){
pe !== 'image/jpeg' && pe !== 'image/png' && pe !== 'image/gif'){
alert('不是有效的图⽚⽂件!');
return;
}
this.imgInfo = Object.assign({}, this.imgInfo, {
name: inputFile.name,
size: inputFile.size,
lastModifiedDate: LocaleString()
inputtypefile样式})
const reader = new FileReader();
that.imgSrc = sult;
}
} else {
return;
}
}
补充知识: vue下打包时⼏个⽂件选择⽂件打包⼀起并做懒加载
直接上代码
const DeviceManage = r => sure([], () => r(require(deviceManagePath + 'main/DeviceManage')), 'g-DeviceManage');
const SingleDeviceSet = r => sure([], () => r(require(deviceManagePath + 'deviceSet/SingleDeviceSet')), 'g-DeviceManage');
const ModifyNo = r => sure([], () => r(require(deviceManagePath + 'modifyNo/ModifyNo')), 'g-DeviceManage');
const PricePerTime = r => sure([], () => r(require(deviceManagePath + 'pricePerTime/PricePerTime')), 'g-DeviceManage');
const SetParams = r => sure([], () => r(require(deviceManagePath + 'setParams/SetParams')), 'g-DeviceManage');
const ShowDevicePrice = r => sure([], () => r(require(deviceManagePath + 'showDevicePrice/ShowDevicePrice')), 'g-DeviceManage');
const parameterSetting = r => sure([], () => r(require(deviceManagePath + 'parameterSetting/parameterSetting')), 'g-DeviceManage');
const SetParams3G = r => sure([], () => r(require(deviceManagePath + 'setParams3G/SetParams3G')), 'g-Device3Gparams');
这么写所有g-DeviceManage的⽂件会被打包在⼀起
以上这篇Vue触发input选取⽂件点击事件操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。