美文网首页
js读取图片文件

js读取图片文件

作者: 1263536889 | 来源:发表于2021-02-22 15:01 被阅读0次

<template>
<div ref="load">
<input type="file"
ref="fileBtn"
@change="uploadImg" />
<img :src="url"
class="img"
ref="img" />

</div>
</template>
<script>
import _ from 'lodash';

export default {
data() {
return {
url: '',
};
},
mounted() {},
methods: {
async uploadImg() {
const inputFile = await this.$refs.fileBtn.files[0];
let res;
if (inputFile) {
if (inputFile.type !== 'image/jpeg' && inputFile.type !== 'image/png' && inputFile.type !== 'image/gif') {
alert('不是有效的图片文件!');
return;
}
const reader = new FileReader();
reader.readAsDataURL(inputFile);
reader.onloadend = () => {
this.url = reader.result.substring(0);
};
} else {
return;
}
},
},
};
</script>
<style scoped lang="less" >
</style>

相关文章

网友评论

      本文标题:js读取图片文件

      本文链接:https://www.haomeiwen.com/subject/pczbmctx.html