vue使用ant design vue(upload)文件上传

使用remove的时候是一个函数,刚开始我用的@remove后来用上:remove才可以,因为接收的是一个(点击移除文件时的回调,返回值为 false 时不移除。支持返回一个 Promise 对象,Promise 对象 resolve(false) 或 reject 时不移除。)文档原话,刚开始不知道返回值是什么,蒙了,返回值就是return。

 <a-upload
   :action="getUploadAction()"
   list-type="picture"
   class="upload-list-inline"
   @change="handleChange"
   :headers="tokenHeader"
   v-model="fileList"
   :remove='removePhtoto'
   :beforeUpload="beforeUpload"
   :default-file-list="item.fileList"
   @preview="preview"
 >
removePhtoto() {
  if (this.status_dictText == '已完成') {
    this.$message.warning('查看状态下只能查看不能编辑!');
    // 直接return一个false就可以了。
    return false;
  }
}

默认显示后端传过来数据的时候,默认是显示不出来的,一般的逻辑就是循环出来以后然后跟上路径就好了,但是组件中说默认显示的是对象或者数组,因为在后端传过来的数据和组件里面的东西不一样,所以循环这个数组的同时,把里面的每一项的值赋给这个数组。

// 主要就是这几个
 detailListItem.fileList.forEach(elementItem => {
   elementItem.uid = elementItem.id
   elementItem.name = elementItem.fileName
   elementItem.url = elementItem.filePath
   elementItem.status = 'done'
 })

在用upload上传change事件的时候一定要做判断,因为change的状态是有三个的,在done的状态下使用就可以了。在使用change时间的时候一定要加一个判断,要不然就调用三次。

 handleChange(info,index,indexcin) {
   // console.log(JSON.parse(JSON.stringify(info)))
   // 加上这个判断就好了。
   if (info.file.status !== 'done') return
   const target = this.dabuleList[index].detailList[indexcin]
   const getFile = {}
   const file = info.file
   file.filePath = file.response ? file.response.message : null
   file.fileName = file.name
   file.relationId = target.id
   this.dabuleList[index].detailList[indexcin].fileList = target.fileList.concat(file)
 },

当你删除项目照片的时候,你需要删除循环出来的每一项的图片的时候,但是你的点击事件就有一个,你删除一个的时候,所有的就都删除了,那你就得通过他的下标删除。

这俩个都可以的,但是我以前用第二个可以,然后不知道怎么出问题了,删除一个的时候就都删除了,然后用的第一个就好了。
this.dabuleList[fileIndex].detailList[fileLIstIndex].fileList.slice(fileLIstIndex,1);

this.dabuleList[fileIndex].detailList[fileLIstIndex].fileList.pop()
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用 Ant DesignUpload 组件可以上传文件,并且可以通过一些配置来获取上传的文件数据。如果上传的文件是 Excel 文件,我们可以使用 js-xlsx 库来读取 Excel 数据。 下面是一个示例代码: ```vue <template> <div> <a-upload accept=".xls,.xlsx" :before-upload="beforeUpload" :on-success="onSuccess" > <a-button> <a-icon type="upload" /> 点击上传 </a-button> </a-upload> <div v-if="excelData"> <h3>Excel 数据</h3> <table> <thead> <tr> <th v-for="(value, key) in excelData[0]">{{ key }}</th> </tr> </thead> <tbody> <tr v-for="row in excelData"> <td v-for="value in row">{{ value }}</td> </tr> </tbody> </table> </div> </div> </template> <script> import XLSX from 'xlsx'; export default { data() { return { excelData: null, }; }, methods: { beforeUpload(file) { // 只处理 Excel 文件 const isExcel = file.type === 'application/vnd.ms-excel' || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; if (!isExcel) { this.$message.error('只能上传 Excel 文件!'); return false; } }, onSuccess(response) { // 读取 Excel 数据 const workbook = XLSX.read(response.file.response, { type: 'array' }); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; const data = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 第一行为表头,去掉 data.shift(); this.excelData = data; }, }, }; </script> ``` 在这个示例代码中,我们使用Ant DesignUpload 组件,并通过 `accept` 属性限制只能上传 Excel 文件。在上传之前,我们可以通过 `before-upload` 属性来判断是否是 Excel 文件,并在不是 Excel 文件的情况下给用户提示错误信息。 在上传成功后,我们可以通过 `on-success` 属性来获取上传的文件信息。`response.file.response` 是上传成功后服务器返回的文件内容,我们使用 js-xlsx 库来读取 Excel 数据,然后将数据显示在页面上。 需要注意的是,这里使用了 `XLSX.utils.sheet_to_json` 方法将 Excel 表格转换成 JSON 格式的数据,但是这个方法只能转换表格中有数据的部分,如果表格中有合并单元格等特殊情况,可能会出现转换错误的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好苦呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值