el-upload自定上传列表删除,上传列表已删除,提交数据仍存在问题

 Element UI 中,el-upload 组件支持通过插槽(slot)来自定义文件列表的展示方式。这通常是通过 file-list 插槽来实现的。下面是一个使用 el-upload 组件并通过 file-list 插槽来自定义文件列表展示的完整示例代码。

在这个示例中,我将展示如何自定义每个文件的显示方式,包括文件名、文件大小、上传进度和删除操作。

<template>  
  <div>  
    <el-upload  
      ref="upload_attach"
      class="upload-demo"  
      action="你的文件上传接口URL"  
      :on-preview="handlePreview"  
      :on-remove="handleRemove"  
      :file-list="fileList"  
      :auto-upload="true" <!-- 设置为true以自动上传文件 -->  
      :on-change="handleChange"  
      :on-progress="handleProgress" <!-- 监听上传进度 -->  
      multiple  
    >  
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>  
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>  
  
      <!-- 自定义文件列表 -->  
      <div slot="file" slot-scope="{file}">
          <div class="file-name">{{ file.name }}</div>  
          <div class="file-size">{{ formatFileSize(file.size) }}</div>  
          <div v-if="file.percentage" class="file-progress">{{ file.percentage }}%</div>  
          <el-button  
            size="mini"  
            type="danger"  
            @click="$refs.upload_attach.handleRemove(file)"  
          >删除</el-button>  
      </div>  
    </el-upload>  
  </div>  
</template>  
  
<script>  
export default {  
  data() {  
    return {  
      fileList: []  
    };  
  },  
  methods: {  
    handlePreview(file) {  
      console.log('preview', file);  
    },  
    handleRemove(fileName, fileList, index) {  
      this.fileList = fileList // 注意更新绑定的fileList,否则已删除的数据仍存在问题
    },  
    handleChange(file, fileList) {  
      // 这里通常不需要手动修改fileList,除非你有特殊的处理逻辑  
    },  
    handleProgress(event, file, fileList) {  
      // 更新文件的上传进度  
      for (let i = 0; i < fileList.length; i++) {  
        if (fileList[i].raw === file.raw) {  
          fileList[i].percentage = event.percent; // 假设event.percent是上传进度百分比  
          break;  
        }  
      }  
    },  
    formatFileSize(size) {  
      if (size / 1024 > 1024) {  
        return (size / 1024 / 1024).toFixed(2) + ' MB';  
      } else {  
        return (size / 1024).toFixed(2) + ' KB';  
      }  
    }  
  }  
};  
</script>  
  
<style>  
.upload-file-list .upload-file-item {  
  margin-top: 10px;  
  display: flex;  
  justify-content: space-between;  
  align-items: center;  
}  
  
.upload-file-list .file-name,  
.upload-file-list .file-size {  
  margin-right: 10px;  
}  
</style>

 在上述代码中,直接调用上传组件源码中的handleRemove,去删除自定义slot中的file

 <!-- 自定义文件列表 -->  
          <div slot="file" slot-scope="{file}">
          <div class="file-name">{{ file.name }}</div>  
          <div class="file-size">{{ formatFileSize(file.size) }}</div>  
          <div v-if="file.percentage" class="file-progress">{{ file.percentage }}%</div>  
          <el-button  
            size="mini"  
            type="danger"  
            @click="$refs.upload_attach.handleRemove(file)"  
          >删除</el-button>  
      </div>  

一定要注意,使用这个$refs.upload_attach.handleRemove(file)删除上传的文件列表,并不会主动的删除绑定的:file-list="fileList" 中fileList的数据,需要配合:on-remove="handleRemove" ,在on-remove中重新给fileList赋值,否则上传时会出现,上传列表中文件删除,提交的时候仍然存在的问题

在Vue中关闭弹窗时手动删除el-upload上传的文件,并清空上传文件列表,可以使用以下步骤: 1. 在关闭弹窗前记录已上传的文件信息,包括文件名、文件地址等。 2. 在关闭弹窗时,手动删除上传的文件。可以通过发起AJAX请求将文件从服务器上删除,或者通过发送DELETE请求将文件从el-upload组件的文件列表删除。 3. 清空上传文件列表。可以通过设置el-upload组件的文件列表为空数组来清空上传文件列表。 下面是一个示例代码,实现了在关闭弹窗时手动删除el-upload上传的文件,并清空上传文件列表的功能: ``` <template> <el-dialog :visible.sync="dialogVisible" title="上传文件"> <el-upload ref="upload" class="upload-demo" :action="uploadUrl" :on-success="handleUploadSuccess" :file-list="fileList" :auto-upload="false" :show-file-list="true" > <el-button slot="trigger" type="primary">选取文件</el-button> <el-button slot="append" type="primary" @click="handleUpload">上传</el-button> </el-upload> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="handleClose">确 定</el-button> </div> </el-dialog> </template> <script> export default { data() { return { dialogVisible: false, fileList: [], uploadedFiles: [] }; }, methods: { handleUploadSuccess(response, file, fileList) { let uploadedFile = { name: file.name, url: response.url }; this.uploadedFiles.push(uploadedFile); }, handleUpload() { this.$refs.upload.submit(); }, handleClose() { // 手动删除上传的文件 this.uploadedFiles.forEach(file => { this.$http.delete(file.url).then(() => { console.log('文件删除成功'); }).catch(() => { console.log('文件删除失败'); }); }); // 清空上传文件列表 this.fileList = []; this.uploadedFiles = []; this.dialogVisible = false; } } }; </script> ``` 在上述示例代码中,我们通过定义一个`uploadedFiles`数组来记录已上传的文件信息。在上传成功回调函数中,将上传的文件信息添加到`uploadedFiles`数组中。 在关闭弹窗时,我们遍历`uploadedFiles`数组,通过发送DELETE请求将文件从服务器上删除。然后,我们通过将`fileList`和`uploadedFiles`数组都清空来清空上传文件列表。最后,关闭弹窗。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值