mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-03 16:39:41 +08:00
111 lines
2.3 KiB
Markdown
111 lines
2.3 KiB
Markdown
|
---
|
||
|
order: 7
|
||
|
title:
|
||
|
zh-CN: 手动上传
|
||
|
en-US: Upload manually
|
||
|
---
|
||
|
|
||
|
## zh-CN
|
||
|
|
||
|
`beforeUpload` 返回 `false` 后,手动上传文件。
|
||
|
|
||
|
## en-US
|
||
|
|
||
|
Upload files manually after `beforeUpload` returns `false`.
|
||
|
|
||
|
````jsx
|
||
|
import { Upload, Button, Icon, message } from 'antd';
|
||
|
import reqwest from 'reqwest';
|
||
|
|
||
|
class Demo extends React.Component {
|
||
|
state = {
|
||
|
fileList: [],
|
||
|
uploading: false,
|
||
|
}
|
||
|
|
||
|
handleUpload = () => {
|
||
|
const { fileList } = this.state;
|
||
|
const formData = new FormData();
|
||
|
fileList.forEach((file) => {
|
||
|
formData.append('files[]', file);
|
||
|
});
|
||
|
|
||
|
this.setState({
|
||
|
uploading: true,
|
||
|
});
|
||
|
|
||
|
// You can use any AJAX library you like
|
||
|
reqwest({
|
||
|
url: '//jsonplaceholder.typicode.com/posts/',
|
||
|
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.');
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
const { uploading } = this.state;
|
||
|
const props = {
|
||
|
action: '//jsonplaceholder.typicode.com/posts/',
|
||
|
onRemove: (file) => {
|
||
|
this.setState(({ fileList }) => {
|
||
|
const index = fileList.indexOf(file);
|
||
|
const newFileList = fileList.slice();
|
||
|
newFileList.splice(index, 1);
|
||
|
return {
|
||
|
fileList: newFileList,
|
||
|
};
|
||
|
});
|
||
|
},
|
||
|
beforeUpload: (file) => {
|
||
|
this.setState(({ fileList }) => ({
|
||
|
fileList: [...fileList, file],
|
||
|
}));
|
||
|
return false;
|
||
|
},
|
||
|
fileList: this.state.fileList,
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<div>
|
||
|
<Upload {...props}>
|
||
|
<Button>
|
||
|
<Icon type="upload" /> Select File
|
||
|
</Button>
|
||
|
</Upload>
|
||
|
<Button
|
||
|
className="upload-demo-start"
|
||
|
type="primary"
|
||
|
onClick={this.handleUpload}
|
||
|
disabled={this.state.fileList.length === 0}
|
||
|
loading={uploading}
|
||
|
>
|
||
|
{uploading ? 'Uploading' : 'Start Upload' }
|
||
|
</Button>
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ReactDOM.render(<Demo />, mountNode);
|
||
|
````
|
||
|
|
||
|
````css
|
||
|
.upload-demo-start {
|
||
|
margin-top: 16px;
|
||
|
}
|
||
|
````
|