ANTD文件上传

ANTD文件上传

文件先上传到一个中间的文件服务器上,然后返回一个http的文件地址,存储时再调用相应后端接口进行文件地址的保存

<a-upload
  name="upload_file"
  :multiple="true"
  :openFileDialogOnClick="true"
  :file-list="fileList"
  :action="'/file/upload/v4'"
  :headers="headers"
  :showUploadList="false"
   @change="uploadChange"
 >
    <a-button  :disabled='uploadLoading'  @click="uploadById(Id)"> <a-icon type="upload" />上传		附件
    </a-button>
</a-upload>

action:路径需要根据自己需要进行配置

export default {
  data() {
    return {
      headers: {
        headers:{设置上传的请求头部},
        fileList:[],//用来绑定上传文件列表
        fileData:[],//作为中间件对上传的文件列表进行筛选
      },
    };
  },
  methods: {
    // 选择上传之后的操作
    uploadChange(info) {
      let that = this
      let { fileList } = info;
      const status = info.file.status;
      if (status !== "uploading") {
         // 上传中
        that.uploadLoading = this.$message.loading("文件上传中...")
      }
      if (status === "done") {
          // 上传完成后
        if(this.fileData.length<1){// 这里是上传单个文件,上传多个可把if判断取消
           // 选择自己需要保存的数据进行重组(可以输出一下info看哪些信息是自己需要的)
          this.fileData.push({
            id: info.file.response.data.rows[0].id,
            path: info.file.response.data.rows[0].url,
            name: info.file.response.data.rows[0].fileName,
            size: info.file.response.data.rows[0].fileSize,
          });
          // 用来保存到数据库
          that.$post({
            url: "/api/slaughter_product/common/myBaseFile/addFileList",
            data: {bid: this.uploadOrderNo, fileList: this.fileData}
          }).then(res=>{
            if(res.rel){
              this.$message.success('上传成功')
              setTimeout(that.uploadLoading,0)
              // 如果需要在上传后进行置空要在这里进行操作(主要表格列表里面会用到此操作)
              // this.fileList = []
              // this.fileData = []
            }
          })
        }else{
          this.$message.warning('请等待上个文件上传')
        }
        
      }else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} 上传失败.`);
      }
      this.fileList = [...fileList]; //重点(因上传的fileList需要使用三次,因此需要此操作)
    },
  },
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值