reactjs - Antd中如何获取form.item中Upload的值?

标签 reactjs antd

即使文件已上传 antd Form.item 规则也会抛出错误。这意味着它没有从上传到 form.item 获取值我应该如何解决这个错误。注意我使用的是 antd 最新版本 4.x,其中 getFieldDecorator 已被弃用。我也想手动上传文件。

import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Alert, Upload } from 'antd';
import { UserOutlined, LockOutlined, UploadOutlined } from '@ant-design/icons';

import styles from './test1.module.css';
const Test1 = () => {
  const [form] = Form.useForm();
  const [state, setState] = useState({
    fileList: [
      {
        thumbUrl:
          'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
      },
    ],
  });

  const handleChange = (info) => {
    console.log(info);
    let fileList = [...info.fileList];

    // 1. Limit the number of uploaded files
    // Only to show two recent uploaded files, and old ones will be replaced by the new
    fileList = fileList.slice(-1);
    setState({ fileList: fileList });
  };

  const onFinish = (values) => {
    console.log(form.getFieldsValue());
  };
  return (
    <div className={styles.login_page}>
      <Form
        name='normal_login'
        className={styles.form}
        form={form}
        onFinish={onFinish}
      >
        <Form.Item
          name='email'
          rules={[
            {
              required: true,
              message: 'Please input your Email!',
            },
          ]}
        >
          <Input
            prefix={<UserOutlined className='site-form-item-icon' />}
            placeholder='Email'
          />
        </Form.Item>
        <Form.Item
          name='password'
          rules={[
            {
              required: true,
              message: 'Please input your Password!',
            },
          ]}
        >
          <Input
            prefix={<LockOutlined className='site-form-item-icon' />}
            type='password'
            placeholder='Password'
          />
        </Form.Item>
        <Form.Item
          name='image'
          rules={[
            {
              required: true,
              message: 'input Image',
            },
          ]}
        >
          <Upload
            beforeUpload={(file) => {
              // console.log(file);
              return false;
            }}
            onChange={handleChange}
            multiple={false}
            listType='picture'
            defaultFileList={state.fileList}
          >
            <Button icon={<UploadOutlined />}>Click to Upload</Button>
          </Upload>
        </Form.Item>

        <Form.Item>
          <Button type='primary' htmlType='submit'>
            Log in
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
};
export default Test1;

有什么方法可以将 Upload 的 onChange 事件监听器与 form.Item 绑定(bind)

最佳答案

Form.Item 中使用 prop getValueFromEvent 并传递返回文件对象的函数。

const getFile = (e) => {
  console.log('Upload event:', e);

  if (Array.isArray(e)) {
    return e;
  }
 return e && e.fileList;
};

Form.Item for image 会像这样

<Form.Item name='image' getValueFromEvent={getFile}>
         <Upload >
         ....
         </Upload>
</Form.Item>

并且你可以在onFinish中得到所有的表单值

const onFinish = (values) => {
    console.log(values)
}

进一步引用this

关于reactjs - Antd中如何获取form.item中Upload的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66670744/

相关文章:

javascript - 如何将react-hooks与Jest和react-testing-library一起使用

reactjs - antd 如何隐藏日期选择器的输入框?

reactjs - Webpack gzip 包 - 未捕获的语法错误 : Unexpected token <

javascript - 为列表中的每个对象进行 api 调用

javascript - 使用 Material 表库时如何更改删除过滤器图标?

html - 从 Layout 组件对齐 Antd 的右菜单

html - 具有垂直和水平滚动的 Ant Design Table

node.js - npm 开始显示奇怪的错误 1

javascript - 将函数存储在 Redux 存储中的替代方法是什么

reactjs - antd 切换勾选属性