zoukankan      html  css  js  c++  java
  • react antd 表单里的上传图片

    import React, { Component } from 'react'
    import { Form, Upload, Button, message } from 'antd';
    
    export default @Form.create()
    class ImgUpload extends Component {
    
      handleSubmit = e => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
          if (!err) {
            console.log(values);
          }
        })
      }
      // 上传之前做一些验证
      beforeUpload = e => {
        if( e.type != 'image/png' ) {
          message.error('格式不对')
          return false
        }
      }
      // 准备上传,上传中,上传后
      normFile = e => {
        if( e.file.type != 'image/png' ) return false //这里也做一个验证
        if ( Array.isArray(e) ) return e
        return e && e.fileList;
      }
    
      render() {
        const { getFieldDecorator } = this.props.form
        return (
          <div className='page_imgUpload'>
            <Form onSubmit={this.handleSubmit}>
              <Form.Item label="Upload">
                {getFieldDecorator('upload', {
                  valuePropName: 'fileList',
                  getValueFromEvent: this.normFile,
                })(
                  <Upload 
                    name="file" //这个name 字段是后台接口的一个参数名,必填
                    action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                    beforeUpload = {this.beforeUpload}  //上传前的回调,用来处理用户格式不正确的逻辑
                  >
                    <Button>
                      Click to upload
                    </Button>
                  </Upload>,
                )}
              </Form.Item>
              <Form.Item>
                <Button type="primary" htmlType="submit">
                  Submit
                </Button>
              </Form.Item>
            </Form>
          </div>
        )
      }
    }
    

      

  • 相关阅读:
    QAU 18校赛 J题 天平(01背包 判断能否装满)
    滚动数组
    Leapin' Lizards HDU
    Sabotage UVA
    Food HDU
    两个Button 加通一个手势,那么 第一个的就失效了,
    终于把聊天记录保存到数据库当中去了,
    添加好友,
    XEP 0055 (Jabber search) with iphone SDK
    手机ip,
  • 原文地址:https://www.cnblogs.com/yetiezhu/p/12913160.html
Copyright © 2011-2022 走看看