2017-07-14 15:00:14 +08:00
|
|
|
---
|
|
|
|
order: 7
|
|
|
|
title:
|
|
|
|
zh-CN: 手动上传
|
|
|
|
en-US: Upload manually
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
`beforeUpload` 返回 `false` 后,手动上传文件。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Upload files manually after `beforeUpload` returns `false`.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2019-08-13 14:07:17 +08:00
|
|
|
import { Upload, Button, message } from 'antd';
|
|
|
|
import { Upload as IconUpload } from '@ant-design/icons';
|
2017-07-14 15:00:14 +08:00
|
|
|
import reqwest from 'reqwest';
|
|
|
|
|
|
|
|
class Demo extends React.Component {
|
|
|
|
state = {
|
|
|
|
fileList: [],
|
|
|
|
uploading: false,
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-07-14 15:00:14 +08:00
|
|
|
|
|
|
|
handleUpload = () => {
|
|
|
|
const { fileList } = this.state;
|
|
|
|
const formData = new FormData();
|
2019-05-07 14:57:32 +08:00
|
|
|
fileList.forEach(file => {
|
2017-07-14 15:00:14 +08:00
|
|
|
formData.append('files[]', file);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
uploading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// You can use any AJAX library you like
|
|
|
|
reqwest({
|
2019-04-30 16:10:00 +08:00
|
|
|
url: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
|
2017-07-14 15:00:14 +08:00
|
|
|
method: 'post',
|
|
|
|
processData: false,
|
|
|
|
data: formData,
|
|
|
|
success: () => {
|
|
|
|
this.setState({
|
|
|
|
fileList: [],
|
|
|
|
uploading: false,
|
|
|
|
});
|
|
|
|
message.success('upload successfully.');
|
|
|
|
},
|
|
|
|
error: () => {
|
|
|
|
this.setState({
|
|
|
|
uploading: false,
|
|
|
|
});
|
|
|
|
message.error('upload failed.');
|
|
|
|
},
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-07-14 15:00:14 +08:00
|
|
|
|
|
|
|
render() {
|
2018-11-19 18:52:47 +08:00
|
|
|
const { uploading, fileList } = this.state;
|
2017-07-14 15:00:14 +08:00
|
|
|
const props = {
|
2019-05-07 14:57:32 +08:00
|
|
|
onRemove: file => {
|
|
|
|
this.setState(state => {
|
2018-11-19 18:52:47 +08:00
|
|
|
const index = state.fileList.indexOf(file);
|
|
|
|
const newFileList = state.fileList.slice();
|
2017-07-14 15:00:14 +08:00
|
|
|
newFileList.splice(index, 1);
|
|
|
|
return {
|
|
|
|
fileList: newFileList,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
2019-05-07 14:57:32 +08:00
|
|
|
beforeUpload: file => {
|
2018-11-19 18:52:47 +08:00
|
|
|
this.setState(state => ({
|
|
|
|
fileList: [...state.fileList, file],
|
2017-07-14 15:00:14 +08:00
|
|
|
}));
|
|
|
|
return false;
|
|
|
|
},
|
2018-11-19 18:52:47 +08:00
|
|
|
fileList,
|
2017-07-14 15:00:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Upload {...props}>
|
|
|
|
<Button>
|
2019-08-13 14:07:17 +08:00
|
|
|
<IconUpload /> Select File
|
2017-07-14 15:00:14 +08:00
|
|
|
</Button>
|
|
|
|
</Upload>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={this.handleUpload}
|
2018-11-19 18:52:47 +08:00
|
|
|
disabled={fileList.length === 0}
|
2017-07-14 15:00:14 +08:00
|
|
|
loading={uploading}
|
2018-11-19 18:52:47 +08:00
|
|
|
style={{ marginTop: 16 }}
|
2017-07-14 15:00:14 +08:00
|
|
|
>
|
2019-05-07 14:57:32 +08:00
|
|
|
{uploading ? 'Uploading' : 'Start Upload'}
|
2017-07-14 15:00:14 +08:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|