123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import React, { useState } from 'react';
- import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
- import Upload from './Upload';
-
- function beforeUpload(file) {
- const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'||file.type==='image/gif';
- if (!isJpgOrPng) {
- message.error('请上传 JPG , PNG或GIF 图片!');
- }
- const isLt10M = file.size / 1024 / 1024 < 10;
- if (!isLt10M) {
- message.error('图片大小必须小于 10MB!');
- }
-
- return isJpgOrPng && isLt10M;
- }
-
- const UploadButton = (props) => (
- <div>
- {props.loading ? <LoadingOutlined /> : <PlusOutlined />}
- <div style={{ marginTop: 8 }}>上传</div>
- </div>
- );
-
- export default (props) => {
- const { value, onChange } = props;
-
- const [loading, setLoading] = useState(false);
-
- const handleChange = info => {
- if (info.file.status === 'uploading') {
- setLoading(true);
- return;
- }
- if (info.file.status === 'error') {
- setLoading(false);
- return;
- }
-
- if (info.file.status === 'done') {
- setLoading(false);
- onChange(info.file.response);
- }
- };
-
- return (
- <Upload
- listType="picture-card"
- className="image-uploader"
- showUploadList={false}
- beforeUpload={beforeUpload}
- onChange={handleChange}
- >
- {value ? <img src={value} alt="avatar" style={{ width: '100%', height: '100%' }} /> : <UploadButton loading={loading} />}
- </Upload>
- );
- }
|