导入,上传 import {Upload} from 'antd'

本文详细介绍了如何在Ant Design框架中利用`import { Upload } from 'antd'`导入并使用Upload组件进行文件上传操作,包括基本用法、上传参数配置和自定义样式等关键点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import { getToken } from '@/utils/cookies';
import defaultSetting from '@/defaultSetting';

const self = this;
    function beforeUpload(file) {
    //格式自选,类型自己定义
      const isXlsx= file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
      if (!isXlsx) {
        message.error('只能上传 xlsx 文件!');
      }
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isLt5M) {
        message.error('Xlsx 上传不能超过 5MB!');
      }
      return isLt5M&&self.setState({loading:false})&&isXlsx;
    }

    const action = `${defaultSetting[process.env.NODE_ENV].baseUrl}${defaultSetting.proxyUrl}/report/importAttendanceRecords`;
    const props = {
      name: 'file',
      action, // 上传地址!!!!!
      headers: {
        Authorization: getToken(),
      },
      beforeUpload,
      // accept:".xls , .xlsx",
      onChange(info) {
        self.setState({
          loading:true
        })
        console.log(info.file.status,"info.file.status")
        const isLt2M = info.file.size / 1024 / 1024 < 5;
        if (info.file.status !== 'uploading') {
          console.log(info.file, info.fileList);
          // if (info.file && info.file.response && info.file.response.code === 200) {
          //   // dispatch({
          //   //   type: 'statemanagement/list',
          //   //   data:info.file.response.data
          //   // })
          // }else
          if(info.file && info.file.response && info.file.response.code !== 200){
            // const data =info.file&&info.file.response&&info.file.response.data&&info.file.response.data;
            // console.log(data,"data");
            self.setState({
              loading:false,
              visible: true,
              ModalText: info.file.response.data ? info.file.response.data :[]
            })
          }

        }
        if (info.file.status === 'done' && info.file.response && info.file.response.code === 200) {
          message.success(`${info.file.name} 导入成功`);
          self.setState({
            loading:false
          })
        } else if (info.file.status === 'error' ||  info.file.response && info.file.response.code !== 200 || !isLt2M) {
          message.error(`${info.file.name} 导入失败`);
          self.setState({
            loading:false
          })
        }
      },
    };
    
<Upload className={styles.Upload} {...props}><Button type="danger" style={{float:"right",background:"rgba(122,135,185,1)",color:"#fff",border:"none"}} icon="download" loading={this.state.loading}>导入特殊工时</Button></Upload>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值