antd 上传进度_react上传文件显示上传进度

import React from 'react';

import'antd/dist/antd.css';

import { Upload, message, Button, Progress } from'antd';

import { UploadOutlined } from'@ant-design/icons';

import axios from"axios"axios.defaults.withCredentials= trueclass App extends React.Component {

constructor(props) {

super(props)this.state ={

fileList: [],

uploading:false,

filseSize:0,

baifenbi:0}

}//文件上传改变的时候

configs ={

headers: {'Content-Type': 'multipart/form-data'},

withCredentials:true,

onUploadProgress: (progress)=>{

console.log(progress);

let { loaded }=progress

let { filseSize }= this.state

console.log(loaded, filseSize);

let baifenbi= (loaded / filseSize * 100).toFixed(2)this.setState({

baifenbi

})

}

}//点击上传

handleUpload = () =>{

const { fileList }= this.state;

const formData= newFormData();

fileList.forEach(file=>{

formData.append('files[]', file);

});this.setState({

uploading:true,

});//请求本地服务

axios.post("http://127.0.0.1:5000/upload", formData, this.configs).then(res =>{this.setState({

baifenbi:100,

uploading:false,

fileList: []

})

}).finally(log =>{

console.log(log);

})

}

οnchange= (info) =>{if (info.file.status !== 'uploading') {this.setState({

filseSize: info.file.size,

baifenbi:0})

}if (info.file.status === 'done') {

message.success(`${info.file.name} file uploaded successfully`);

}else if (info.file.status === 'error') {

message.error(`${info.file.name} file upload failed.`);

}

}

render() {

const { uploading, fileList }= this.state;

const props={

onRemove: file=>{this.setState(state =>{

const index=state.fileList.indexOf(file);

const newFileList=state.fileList.slice();

newFileList.splice(index,1);return{

fileList: newFileList,

};

});

},

beforeUpload: file=>{this.setState(state =>({

fileList: [...state.fileList, file],

}));return false;

},

fileList,

};return(

{this.state.baifenbi + '%'}

{ this.onchange(e) }} {...props}>

Click to Upload

type="primary"onClick={this.handleUpload}

disabled={fileList.length === 0}

loading={uploading}

style={{ marginTop: 16}}>{uploading? 'Uploading' : 'Start Upload'}

)

}

}

exportdefault App;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值