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赋值,否则上传时会出现,上传列表中文件删除,提交的时候仍然存在的问题

### 使用 `el-upload` 组件获取上传图片数据 为了实现通过 `el-upload` 组件来获取上传图片的数据,在 Vue.js 中可以利用该组件提供的多种属性和事件处理程序。当设置 `auto-upload="false"` 时,能够控制文件的上传行为[^1]。 下面是一个具体的例子展示如何捕获并操作这些图像数据: #### HTML 结构与样式定义 ```html <template> <div id="app"> <!-- 设置 auto-upload 属性为 false 来阻止自动提交 --> <el-upload ref="upload" action="" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" list-type="picture-card" :auto-upload="false" @change="handleChange" > <i class="el-icon-plus"></i> </el-upload> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button> <!-- 图片预览对话框 --> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt /> </el-dialog> </div> </template> <style scoped> /* 自定义样式 */ .el-upload { border: 1px dashed #d9d9d9; border-radius: 6px; cursor: pointer; position: relative; overflow: hidden; } .uoloadSty .el-upload--picture-card{ width:110px; height:110px; line-height:110px; } .disUoloadSty .el-upload--picture-card{ display:none; /* 上传按钮隐藏 */ } </style> ``` #### JavaScript 方法逻辑 ```javascript <script> export default { data() { return { fileList: [], // 存储已选择但未上传的文件列表 dialogImageUrl: '', // 当前显示的大图链接 dialogVisible: false, // 控制大图查看器可见性的布尔值 }; }, methods: { handleChange(file, fileList) { // 文件状态改变时触发 (添加/删除) this.fileList = fileList.slice(-3); // 只保留最近三次的选择记录作为示例限制 }, handleRemove(file, fileList) { // 移除某个文件时触发 console.log('移除了:', file); }, handlePreview(file) { // 查看某张图片详情时触发 this.dialogImageUrl = file.url || URL.createObjectURL(file.raw); this.dialogVisible = true; }, submitUpload() { // 手动点击按钮发起上传请求 const formData = new FormData(); this.$refs.upload.submit(); // 提交表单 // 或者遍历 fileList 并手动构建 formdata 对象用于自定上传流程 this.fileList.forEach((item) => { if(item.raw){ formData.append('files', item.raw); } }); axios.post('/api/upload', formData).then(response => { console.log('成功响应:', response.data); }).catch(error => { console.error('失败原因:', error.message); }); } } }; </script> ``` 此代码片段展示了怎样配置 `el-upload` 组件以及关联的操作函数以管理用户所选文件,并提供了两种方式来进行实际的服务器端通信:一种是直接调用 `.submit()` 方法;另一种则是创建一个新的 `FormData` 实例并将选定文件附加到其中再发送给后端API[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值