Vue触发input选取文件点击事件操作
更新时间:2020年08月07日 11:35:43 作者:LaLaLa_heng
这篇文章主要介绍了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;
}
}
补充知识: 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选取文件点击事件操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Vue3.x如何操作v-html指令中HTML的DOM和样式
在 Vue3.x 中,v-html 指令用于将 HTML 字符串渲染为真实的 DOM 元素,下面我们来看看具体如何操作v-html指令中HTML的DOM和样式吧2025-04-04
图文详解Element-UI中自定义修改el-table样式
elementUI提供的组件间距、样式都比较大,如果直接套用,在页面显示可能就会显得很大,就比如表格,表头、行宽如果不修改的话,遇到列较多的时候,会显得整个页面就不好看,下面这篇文章主要给大家介绍了关于Element-UI中自定义修改el-table样式的相关资料,需要的朋友可以参考下2022-08-08
五步教你用Nginx部署Vue项目及解决动态路由刷新404问题
nginx 是一个代理的服务器,下面这篇文章主要给大家介绍了关于如何通过五步教你用Nginx部署Vue项目及解决动态路由刷新404问题的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下2022-12-12


最新评论