vue input标签及Element-upload 上传图片及转base64

input标签上传图片及转base64:

<img class="appIcon" :src="addForm.appIconStr">
<input ref="addAppIcon" type="file" @change="Tobase64()">

Tobase64() {
    let fileObj = this.$refs.addAppIcon.files[0]
    let reader = new FileReader()
    reader.readAsDataURL(fileObj)
    let _this = this
    reader.onload = function(e) {
       _this.addForm.appIconStr = e.target.result
    }
},

Element-upload 上传图片及转base64:

<el-upload
    ref="upload"
    class="avatar-uploader"
    action="#"
    :http-request="httpRequest"
    :show-file-list="false"
    :before-upload="beforeAvatarUpload">
    <img v-if="addForm.appIconStr" :src="addForm.appIconStr" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>


httpRequest(obj) {
            console.log(obj.file)
            debugger
            let fileObj = obj.file
            let reader = new FileReader()
            reader.readAsDataURL(fileObj)
            let _this = this
            reader.onload = function(e) {
                _this.addForm.appIconStr = e.target.result
            }
        },
        beforeAvatarUpload(file) {
            debugger
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024  < 65;

            if (!isPNG) {
                this.$message.error('上传头像图片只能是 PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 65K!');
            }
            return isPNG && isLt2M;
        },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值