upload上传到后台服务器和上传到阿里云服务器前端处理方法是一样的

这篇博客介绍了如何利用Ant Design的Upload组件来上传Excel文件,包括设置额外的type参数、接受的文件类型、多文件上传、上传动作、授权头以及文件状态的监听方法。同时,还展示了在页面中有多个导入功能时如何区分不同上传状态,并处理上传成功与失败的情况。代码示例中涉及了文件上传至服务器的POST接口以及Formdata的使用。
摘要由CSDN通过智能技术生成
<Upload
                name="file"
//除了参数file: (binary) ,额外添加的type参数
                data={{ type: "excel" }} 
                showUploadList={false}
                accept=".xlsx,.xls"
                multiple
                action={`${window.config.serverOne}/api/v1/business/dict/importDict`}
                method="POST"
                headers={{
                  Authorization: users && `Bearer ${users.access_token}`,
                }}
//这是判断上传文件的状态的方法
                onChange={(info) => this.onChange(info)}
              >
                <Button type="primary" style={{ width: 100, marginLeft: 8, marginRight: 8 }}>导入Excel</Button>
              </Upload>


//监听上传状态
 onChange = async (info: any) => {
    console.log(info, "info---->");
}

// 当一个页面有2个导入时,判断状态可传2个参数

 /** 判断文件上传状态 */
  onChange = async (info: any, idx: number) => {
    if (info?.file?.status === "uploading") {
      if (idx === 1) {
        this.setState({ loadingG: true })
      } else {
        this.setState({ loading: true })
      }
    }
    if (info?.file?.status === "done") {
      if (idx === 1) {
        this.setState({ loadingG: false })
      } else {
        this.setState({ loading: false })
      }
      if (info?.file?.response?.msg === "ok") {
        message.success("文件上传成功")
      } else {
        message.error("文件上传失败")
      }
    } else if (info?.file?.status === "error") {
      message.error("文件上传失败")
      if (idx === 1) {
        this.setState({ loadingG: false })
      } else {
        this.setState({ loading: false })
      }
    }
  }

post接口 ,上传参数在Formdata

切记: upload上传到后台服务器和上传到阿里云服务器都是一样的写法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值