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';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { UploadOutlined } from '@ant-design/icons';
|
2017-07-14 15:00:14 +08:00
|
|
|
|
|
|
|
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
|
2021-12-03 23:54:19 +08:00
|
|
|
fetch('https://www.mocky.io/v2/5cc8019d300000980a055e76', {
|
|
|
|
method: 'POST',
|
|
|
|
body: formData,
|
|
|
|
})
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(() => {
|
2017-07-14 15:00:14 +08:00
|
|
|
this.setState({
|
|
|
|
fileList: [],
|
|
|
|
});
|
|
|
|
message.success('upload successfully.');
|
2021-12-03 23:54:19 +08:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
message.error('upload failed.');
|
|
|
|
})
|
|
|
|
.finally(() => {
|
2017-07-14 15:00:14 +08:00
|
|
|
this.setState({
|
|
|
|
uploading: false,
|
|
|
|
});
|
2021-12-03 23:54:19 +08:00
|
|
|
});
|
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 (
|
2020-07-08 08:58:45 +08:00
|
|
|
<>
|
2017-07-14 15:00:14 +08:00
|
|
|
<Upload {...props}>
|
2020-08-24 16:33:24 +08:00
|
|
|
<Button icon={<UploadOutlined />}>Select File</Button>
|
2017-07-14 15:00:14 +08:00
|
|
|
</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>
|
2020-07-08 08:58:45 +08:00
|
|
|
</>
|
2017-07-14 15:00:14 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default Demo;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|