记Ant Design Vue upload文件修改文件名称

界面样式

界面实现代码

<template>
  <a-modal
    title="Upload Stuffing Plan by Release Key"
    :width="757"
    :visible="visible"
    :confirmLoading="uploading" //等待
    okText="Next" 
    @ok="onSubmit"
    @cancel="
      () => {
        $emit('cancel') //父类方法
      }
    "
    :keyboard="false"
    :maskClosable="false"
  >
    <a-spin :spinning="uploading">
      <div class="upload-content-mdl">
        <a-row type="flex">
          <a-col class="upload-left">
            <img class="upload-left-img" src="@/assets/min/up.png" alt="" />
          </a-col>
          <a-col class="upload-right">
            <h1>Upload the completed data sheet</h1>
            <p>The file suffix must be XLS or XLSX (Excel format), the file size must not be larger than 10M.</p>
            //fileList 放置文件位置
            //beforeUpload 文件发送后端前执行方法
            //RemoveFile 清空fileList中文件
            <a-upload style="margin-top: -5px" :file-list="fileList" :before-upload="beforeUpload" :remove="RemoveFile">
              <a v-if="fileList.length < 1">Upload a file</a>
            </a-upload>
          </a-col>
        </a-row>
      </div>
    </a-spin>
  </a-modal>
</template>

 文件发送给后端前修改文件名称(处理文件包含 ' ,导致不能正常上传)

**file 是没有 set 属性的,只有 get**

两种处理方式:

  •  在 beforeUpload() 方法中对文件重新命令

        var file = new File([file], "xxx");  --  后面是自定义名称

  • 在 onSubmit() 文件发送给后端时进行处理

onSubmit() {
      const { fileList } = this
      const formData = new FormData()
      //获取文件名称
      let names = fileList[0].name.split(".");
      Object.defineProperty(fileList[0],'name',{
        writable:true,//设置属性为可写
      })
        //处理文件名称
		  fileList[0].name = names[0].replaceAll("'","") + "." + names[names.length-1];
      formData.append('releaseKeyExcelFile', fileList[0],fileList[0].name)
      //上传文件到后端
      axios({
        url: xxxx',
        method: 'post',
        processData: false,
        headers: { 'Content-Type': 'multipart/form-data', Authorization: 'Bearer ' + storage.get(ACCESS_TOKEN) },
        data: formData,
      })
        .then((res) => {
          this.fileList = []
          this.$emit('ok')
        })
        .catch((err) => {
          // error
        })
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值