上传图片 vue

Element里面有一个 el-upload 组件

https://element.eleme.cn/2.13/#/zh-CN/component/upload

话不多说直接上代码  可能不是最优但能解决问题就是好方法

<template>
    <div>
        <el-upload action="https://jsonplaceholder.typicode.com/posts/" :limit="2" list-type="picture-card"
            :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :on-success="successfile" :on-error="error"
            :on-exceed="exceed" accept="image/png,image/jpg,image/jpeg,image/gif">
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <img width="50px" height="50px" :src="dialogImageUrl" alt="">
        <button @click="get">获取</button>
    </div>
</template>

<script>
export default {
    name: '',
    data() {
        return {
            dialogImageUrl: '',
            dialogVisible: false,
            imglist: []
        }
    },
    components: {

    },
    methods: {
        // 删除
        handleRemove(file, fileList) {
            console.log(file, fileList);
            this.imglist = []
            for (let i = 0; i < fileList.length; i++) {
                this.baseimg(fileList[i].raw)
            }
        },
        // 点击查看
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        // 成功
        successfile(response, file, fileList) {
            console.log(response, file, fileList)
            this.baseimg(file.raw)
        },
        // 上传失败
        error() {
            console.log(111111111)
            this.$message.error('上传失败');
        },
        // 上传图片超出
        exceed() {
            this.$message.error('上传个数上线');
        },
        get() {
            console.log(this.imglist)
        },
        // 公共转换base64
        baseimg(file) {
            const reader = new FileReader()
            const that = this
            reader.readAsDataURL(file)
            reader.onload = function () { // 图片转base64完成后返回reader对象
                that.imglist.push(reader.result)
            }
        }

    }
}

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值