ant-design/components/upload/demo/picture-card.md

123 lines
3.0 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
2016-11-25 15:00:28 +08:00
order: 3
2016-09-18 11:08:54 +08:00
title:
2016-11-25 15:00:28 +08:00
zh-CN: 照片墙
en-US: Pictures Wall
2016-03-31 09:40:55 +08:00
---
2016-01-15 16:44:07 +08:00
2016-08-25 09:46:19 +08:00
## zh-CN
2016-11-25 15:00:28 +08:00
用户可以上传图片并在列表中显示缩略图。当上传照片数到达限制后,上传按钮消失。
2016-01-15 16:44:07 +08:00
2016-08-25 09:46:19 +08:00
## en-US
2016-11-25 15:00:28 +08:00
After users upload picture, the thumbnail will be shown in list. The upload button will disappear when count meets limitation.
2016-08-25 09:46:19 +08:00
2019-05-07 14:57:32 +08:00
```jsx
2016-04-05 12:30:14 +08:00
import { Upload, Icon, Modal } from 'antd';
2016-01-15 16:44:07 +08:00
2019-05-24 16:36:09 +08:00
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
2016-11-25 15:00:28 +08:00
class PicturesWall extends React.Component {
state = {
previewVisible: false,
previewImage: '',
2019-05-07 14:57:32 +08:00
fileList: [
{
uid: '-1',
2019-07-22 19:18:47 +08:00
name: 'image.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
{
uid: '-2',
name: 'image.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
{
uid: '-3',
name: 'image.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
{
uid: '-4',
name: 'image.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
{
uid: '-5',
name: 'image.png',
2019-05-07 14:57:32 +08:00
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
],
2016-11-25 15:00:28 +08:00
};
2019-05-07 14:57:32 +08:00
handleCancel = () => this.setState({ previewVisible: false });
2016-11-25 15:00:28 +08:00
2019-05-24 16:36:09 +08:00
handlePreview = async file => {
if (!file.url && !file.preview) {
file.preview = await getBase64(file.originFileObj);
}
2016-04-05 12:30:14 +08:00
this.setState({
2019-05-24 16:36:09 +08:00
previewImage: file.url || file.preview,
2016-11-25 15:00:28 +08:00
previewVisible: true,
2016-04-05 12:30:14 +08:00
});
2019-05-07 14:57:32 +08:00
};
2016-11-25 15:00:28 +08:00
2019-05-07 14:57:32 +08:00
handleChange = ({ fileList }) => this.setState({ fileList });
2016-11-25 15:00:28 +08:00
2016-04-05 12:30:14 +08:00
render() {
2016-11-25 15:00:28 +08:00
const { previewVisible, previewImage, fileList } = this.state;
const uploadButton = (
<div>
<Icon type="plus" />
<div className="ant-upload-text">Upload</div>
</div>
);
2016-04-05 12:30:14 +08:00
return (
<div className="clearfix">
2016-11-25 15:00:28 +08:00
<Upload
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
2016-11-25 15:00:28 +08:00
listType="picture-card"
fileList={fileList}
onPreview={this.handlePreview}
onChange={this.handleChange}
2016-11-22 14:21:42 +08:00
>
2019-07-22 19:18:47 +08:00
{fileList.length >= 8 ? null : uploadButton}
2016-11-25 15:00:28 +08:00
</Upload>
<Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
<img alt="example" style={{ width: '100%' }} src={previewImage} />
2016-04-05 12:30:14 +08:00
</Modal>
</div>
);
2016-11-25 15:00:28 +08:00
}
}
2016-01-15 16:44:07 +08:00
2016-11-25 15:00:28 +08:00
ReactDOM.render(<PicturesWall />, mountNode);
2019-05-07 14:57:32 +08:00
```
2016-01-15 16:44:07 +08:00
2019-05-07 14:57:32 +08:00
```css
2016-08-25 09:46:19 +08:00
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
2017-10-26 11:47:05 +08:00
font-size: 32px;
2016-01-15 16:44:07 +08:00
color: #999;
}
.ant-upload-select-picture-card .ant-upload-text {
2016-01-15 16:44:07 +08:00
margin-top: 8px;
color: #666;
}
2019-05-07 14:57:32 +08:00
```