From a9b5acfba9512e57588ffb4775bd98ae907f0fc4 Mon Sep 17 00:00:00 2001 From: zombiej Date: Fri, 24 May 2019 16:36:09 +0800 Subject: [PATCH] update preview demo --- components/upload/demo/picture-card.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/components/upload/demo/picture-card.md b/components/upload/demo/picture-card.md index 6b9e5a9263..bafbb98116 100644 --- a/components/upload/demo/picture-card.md +++ b/components/upload/demo/picture-card.md @@ -16,6 +16,15 @@ After users upload picture, the thumbnail will be shown in list. The upload butt ```jsx import { Upload, Icon, Modal } from 'antd'; +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); + }); +} + class PicturesWall extends React.Component { state = { previewVisible: false, @@ -32,9 +41,13 @@ class PicturesWall extends React.Component { handleCancel = () => this.setState({ previewVisible: false }); - handlePreview = file => { + handlePreview = async file => { + if (!file.url && !file.preview) { + file.preview = await getBase64(file.originFileObj); + } + this.setState({ - previewImage: file.url || file.thumbUrl, + previewImage: file.url || file.preview, previewVisible: true, }); };