Ant Design Pro +React 怎么实现上传
时间: 2024-06-11 11:05:37 浏览: 101
Ant Design Pro React 提供了 Upload 组件来实现上传功能。以下是实现上传的步骤:
1. 导入 Upload 组件和其他必要的组件:
```
import { Upload, message } from 'antd';
import { InboxOutlined } from '@ant-design/icons';
```
2. 定义上传组件的属性和状态:
```
const { Dragger } = Upload;
const props = {
name: 'file',
multiple: true,
action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
onChange(info) {
const { status } = info.file;
if (status !== 'uploading') {
console.log(info.file, info.fileList);
}
if (status === 'done') {
message.success(`${info.file.name} file uploaded successfully.`);
} else if (status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
class UploadComponent extends React.Component {
state = {
fileList: [],
};
handleChange = ({ fileList }) => this.setState({ fileList });
render() {
const { fileList } = this.state;
return (
<Dragger {...props} fileList={fileList} onChange={this.handleChange}>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">Click or drag file to this area to upload</p>
<p className="ant-upload-hint">
Support for a single or bulk upload. Strictly prohibit from uploading company data or other band files
</p>
</Dragger>
);
}
}
```
3. 在 render() 方法中渲染上传组件:
```
render() {
return (
<div>
<UploadComponent />
</div>
);
}
```
这样就可以实现一个基本的上传组件。此外,Ant Design Pro React 还提供了其他的上传组件和功能,可根据具体的需求进行选择和使用。
阅读全文