• antd react 文件上传只允许上传一个文件且上传后隐藏上传按钮


    antd react 文件上传只允许上传一个文件且上传后隐藏上传按钮

    效果图

    在这里插入图片描述
    在这里插入图片描述

    代码解析

    import { Form, Upload, message } from 'antd';
    import { PlusOutlined } from '@ant-design/icons';
    import { useState, useEffect } from 'react';
    import { BASE_URL } from '@/utils/request';
    const FormItemInputUpload = (props) => {
      const [visble, setVisibel] = useState(false);
    
      useEffect(() => {
        if (props.edit && !props?.maxCount) {
           setVisibel(true); //上传一张图片时,已上传隐藏上传图标
        } else{
          if(props.edit.length==props.maxCount){
            setVisibel(true);//上传多张张图片时,到指定个数隐藏上传图标
          }else{
            setVisibel(false);
          }
        }
      }, [props]);
    
      const normFile = (e) => {
        if (e.fileList && e.fileList.length == 0) {
          setVisibel(false);
        } else if (!props?.maxCount) {
          setVisibel(true);
        } else if (props?.maxCount == e.fileList.length) {
          setVisibel(true);
        }
        else {
          setVisibel(false);
        }
        if (Array.isArray(e)) {
          return e;
        }
        return e?.fileList;
      };
    
      const onRemove = (e) => {
        const urls = [props.form.getFieldValue(props.name)]
          .flat()
          .filter((item) => item.response.data[0].imageAddress != e.response.data[0].imageAddress);
        console.log(urls, 'urls');
        props.form.setFieldsValue({
          [props.name]: urls,
        });
      };
      return (
        <Form.Item
          label={props.label}
          name={props.name}
          valuePropName="fileList"
          getValueFromEvent={normFile}
          rules={
            props?.rules && [
              {
                required: true,
                validator: (_, value, callback) => {
                  if (!value || value.length == 0) {
                    callback(`请上传${props.label}`);
                  } else {
                    callback();
                  }
                },
              },
            ]
          }
        >
          <Upload
            maxCount={props?.maxCount || 1}
            action={`${BASE_URL}/cdsj-file/upload`}
            data={{ minioCatalogEnums: props.sysType }}
            name="files"
            headers={{ Authorization: localStorage.getItem('token') }}
            listType="picture-card"
            accept=".png,.jpeg,.jpg"
            beforeUpload={(file) => {
              const isPNG =
                file.type == 'image/png' ||
                file.type == 'image/jpg' ||
                file.type == 'image/jpeg';
              if (!isPNG) {
                message.error('请上传图片格式文件!');
              }
               
              return isPNG || Upload.LIST_IGNORE;
            }}
            onRemove={onRemove}
          >
            {visble ? null : <PlusOutlined />}
          </Upload>
        </Form.Item>
      );
    };
    
    export default FormItemInputUpload;
    
    
    
    ···
    //引用
              <FormItemInputUpload
                  name="image"
                  label="图片"
                  edit={props?.fillingForm?.image}
                  form={form}
                  sysType="SIGNATURE"
                  rules
                   maxCount={2} //可传可不传
                />
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
  • 相关阅读:
    [学习记录] SpringBoot 4. 开发技巧
    最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
    app一键加固加签名脚本 百度加固 window版本
    Springboot 文件下载代码
    【Windows Server 2019】企业虚拟专用网络服务的配置和管理(下)
    房地产小程序 | 小程序赋能,房地产业务数字化升级
    ISCSI:后端卷以LVM 的方式配置 ISCSI 目标启动器
    关于账本数据库:你想知道的这里都有
    什么是DevOps
    vue子组件向父组件传值
  • 原文地址:https://blog.csdn.net/weixin_43787651/article/details/132833524