上传图片转base64

该代码段展示了在Vue.js中使用ElementUI组件进行图片上传的实现,包括限制上传格式、大小以及数量,并通过FileReader接口获取图片的base64编码显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <el-form-item label="图标文件名"
                      label-width="120px">
   <el-input v-model="form.icon"
             autocomplete="off"></el-input>

   <el-upload class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-change="changeFile"
              multiple
              :limit="3"
              :file-list="fileList"
              list-type="picture">
     <el-button size="small"
                type="primary">上传图片</el-button>
     <div slot="tip"
          class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
   </el-upload>
 </el-form-item>
getBase64(file) {
  return new Promise(function (resolve, reject) {
    const reader = new FileReader()
    let imgResult = ''
    reader.readAsDataURL(file)
    reader.onload = function () {
      imgResult = reader.result
    }
    reader.onerror = function (error) {
      reject(error)
    }
    reader.onloadend = function () {
      resolve(imgResult)
    }
  })
},

// 上传图片
changeFile(file, fileList) {
  console.log(file)
  this.getBase64(file.raw).then(res => {
    console.log(res)
    this.form.icon = res
  })
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值