react+antd自定義上傳文件,自定義控制上傳文件個數-upload

  1. 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

     

  2. 有什麼建議能夠提出來哦!
相關文章
相關標籤/搜索