vue+elementUI上传图片/文件,编辑时回显文件名

vue+elementUI上传图片/文件,编辑时回显文件名

回显文件名

HTML代码块:

<el-form-item label="上传" :label-width="formLabelWidth" prop="file">
    <el-upload
     style="width: 80%;"
     v-model='form.file'
     ref="upload"
     class="upload-demo"
     action="/backend/FileLibrary/form/multiple"
     :headers= "headerToken"
     :on-success="uploadSuccess"
     :file-list="fileList"
     :on-change="fileChange"
    >
    <el-button slot="trigger" size="small" type="primary" >选取文件</el-button>
    </el-upload>
</el-form-item>

js代码块:

  1. 上传功能
uploadList: [],
fileList: [],
form:{
	file: [],
}

methods: {
//上传
        fileChange(file, fileList) {
          this.fileList = fileList;
        },
        submitUpload() {
          let param = new FormData();
          console.log(this.fileList)
          this.fileList.forEach(file => {
          param.append("file", file.raw); 
          param.append("name", file.name); 
        });
        
        // this.$refs.upload.submit();
        this.uploadImgList(param)
        },
        uploadImgList(param) {
          const self = this
          let config = {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'token': sessionStorage.getItem('token')
            }
          }
          axios.post("/backend/FileLibrary/form/multiple", param, config).then(res => {
            console.log('res')
            console.log(res)
            self.form.file = res.data.data.webUrlList
          
          }).catch(res => {
            console.log(res)
          })
        },
        uploadSuccess: function(response, file, fileList) {
          console.log(response)
          console.log(file)
          console.log(fileList)
          this.form.file = response.data.webUrlList
          console.log(this.form)
        },
        handleCurrentChange: function (val) {
          this.currentPage4 = val;
          this.getProjectList();
        },
        handleSizeChange: function (val) {
          this.currentPage4 = 1;
          this.pageSize = val;
          this.getProjectList();
        },
}
  1. 获取数据并回显
getProjectList: function () {
          //debugger
          axios.get("后端接口",{
            params: {
              //按需传值
            },
              headers: {
                'token': sessionStorage.getItem('token'),//获取当前token值
                'Content-type': 'Content-Type: multipart/form-data'
              }
            }).then((response) => {
              let vm = this;
              if(response.data.data.records[0].file) { //后台返回的文件链接
                let a = (response.data.data.records[0].file).split(',');
                  if(a.length > 0) {
                    a.forEach(item => {
                        var obj = {} 
                          let index = item.lastIndexOf('\/');
                          let fileName  = item.substring(index + 1, item.length); //最后的文件名截取出来
                          vm.$set(obj,'name',fileName);
                          vm.$set(obj,'url',item);  //修改files里面的结构(name,url)
                          vm.fileList.push(obj);
                      })
                  }
                  
              }
            })
            .catch(function (error) {
              console.log(error)
            });
        },

效果预览:
在这里插入图片描述

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值