Element UI Upload上传图片方法

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

废话不多说直接上代码!!

<el-upload
   class="upload-demo"
   action="onLine/uploadPictures" //上传后台的url路径
   :on-success="handleSuccess" //成功后回调函数
   :on-exceed="addr_addImg"  //超出设置的上限后回调
   :on-remove="uploadDel"  //删除图片的回调函数
   :limit="9" //允许最大上传几张图片
   name="files" //后台接收的字段名称
   list-type="picture-card"
   :file-list="fileList" //图片列表数组
 >
   <div class="flex uploadIcon">
       <i class="el-icon-plus" style="margin-bottom: 10px"></i>
       <div style="line-height: 20px">添加照片</div>
   </div>
</el-upload>

 一定要在data里声明fileList:[];

//上传图片
handleSuccess(res, file, fileList) {
    this.$message({
        message: "上传成功",
        type: "success",
    });
    // console.log(file, "file成功");
    // console.log(fileList, "fileList成功");
    var list = [];
    for (let i = 0; i < fileList.length; i++) {
    // console.log(fileList[i].response.data.servicePersonalWorkPhoto);
        if (!fileList[i].response) {
            list.push(fileList[i].url);
        } else {
            list.push(fileList[i].response.data.fileAddress);
            }
        }
        this.fileList = list;
        // console.log("上传成功后照片列表", this.fileList);
},
//删除图片
uploadDel(file, fileList) {
    // console.log(file, "文件删除file");
    // console.log(fileList, "文件删除fileList");
    var list = [];
    for (let i = 0; i < fileList.length; i++) {
        if (!fileList[i].response) {
            list.push(fileList[i].url);
        } else {
            list.push(fileList[i].response.data.fileAddress);
            }
     }
     // console.log(list, "删除后的数组");
     this.fileList = list.map((item) => {
        return {
            name: item,
            url: item,
        };
     });
    // console.log(this.fileList, "删除后的数组");
    // console.log(file.response.data.fileAddress);
},
// 超出图片上传数量
addr_addImg() {
    this.$message({
        type: "error",
        message: "图片总数超出范围",
    });
},

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值