react+antd自定义上传文件,自定义控制上传文件个数-upload

import React from 'react';
import {
    Upload,
} from 'antd';

class upload extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            fileList:[],     //文件列表
          };
    }
    // 上传之前事件
    beforeUpload = (file) => {
      var fileArr = [];
//获取新的上传列表
      fileArr.push(file);
//进行赋值保存
     this.setState(preState => ({
       fileList:fileArr,
       uploadPath:''
     }))
    }
    // 文件上传改变事件
    updateChange = (info) => {      
        if (info.file.status === 'done') {
//上传成功后将后台返回来的文件地址进行获取--info.file.response
           if (info.file.response) {
             this.setState(preState => ({
              uploadPath : info.file.response.Data,
            })
             )
        }
            message.success('上传成功!');
        } else if (info.file.status === 'error') {
//上传失败进行提示
            message.error('上传失败!');
        }
    }
    // 移除文件
    removeFile = () => {
       this.setState(preState => ({
        fileList:[],
        uploadPath : ‘’
      })
       )
    }
    render() {
        const {
            fileList,
        } = this.state;
       
        return (
            <div>
               
                  
                  
            
                            <Upload 
                            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,"   //上传文件类型--这个是excel类型
                            action = {‘http://dbsahdfff.com/api/upload’}   上传文件地址
                            fileList = {fileList}   //上传文件列表
                            beforeUpload={this.beforeUpload}   //上传之前触发事件
                            onChange={this.updateChange}   //上传状态改变事件
                            onRemove = {this.removeFile}   //移除文件事件
                           >
                                <div>上传文件</div>
                            </Upload>
            </div>
        )
    }
}
export default upload

如果有用就请赞一个哦,谢谢!

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值