el-upload实现上传头像

1.

<template>
   <el-upload
       :action="uploadFileUrl"
        list-type="picture-card"
       :on-preview="handlePictureCardPreview"
       ref="uploadimgs"
       :on-remove="handleRemove"
       :class="{hide:iconUploadShow}"
       :on-change="handleImgChange"
       :on-success="handleUploadSuccess"
       multiple
       :limit="limit"
       :on-exceed="handleExceed"
       :before-upload="handleBeforeUpload">
      <img v-if="plusDialog==false" width="100%" :src="form.photo" alt="" @click="plusDialog=true">
      <i v-else   class="el-icon-plus"></i>
  </el-upload>
</template>
<script>
 export default{
   data(){
   return{
    uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
    fileType: ["bmp", "gif", "jpg", "jpeg", "png"],
    limit:1,
    }
  },
  methods:{
      handlePictureCardPreview(file) {
        this.form.photo = file.url;
        this.dialogVisible = true;
      },
      handleRemove(file, fileList) {
        // let arr = this.fileList
        // let newarr = []
        // for (let item of arr) {
        //   if (file.response.url != item.file_url) {
        //     newarr.push(item)
        //   }
        // }
        // this.fileList = newarr
        // if (fileList.length == 0) {
        //   this.iconUploadShow = false
        // }
      },
      handleImgChange(file, fileList) {
        if (fileList.length >= this.limit) {
          this.iconUploadShow = true
        }
      },
      handleUploadSuccess(res) {
        this.fileList.push({
          file_name: res.fileName,
          name: res.name,
          file_url: res.url,
          type: res.type,
          file_size: res.size
        });
        this.form.photo = this.fileList[0].file_url
        console.log(this.fileList)
        this.$message.success('上传成功!')
        console.log(res)
      },
      handleExceed(files, fileList) {
        this.$message.error("上传数量超过限制");
      },
      
  }
 }
 
</script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值