2016-11-25 15:00:28 +08:00
---
order: 1
title:
zh-CN: 用户头像
en-US: Avatar
---
## zh-CN
点击上传用户头像,并使用 `beforeUpload` 限制用户上传的图片格式和大小。
2021-04-29 11:54:40 +08:00
> `beforeUpload` 的返回值可以是一个 Promise 以支持异步处理,如服务端校验等:[示例](https://upload-react-component.vercel.app/demo/before-upload#beforeupload)。
2016-11-25 15:00:28 +08:00
## en-US
Click to upload user's avatar, and validate size and format of picture with `beforeUpload` .
2021-04-29 11:54:40 +08:00
> The return value of function `beforeUpload` can be a Promise to check asynchronously. [demo](https://upload-react-component.vercel.app/demo/before-upload#beforeupload)
2016-11-25 15:00:28 +08:00
2022-05-19 09:46:26 +08:00
```tsx
2019-11-28 12:34:33 +08:00
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
2022-05-23 14:37:16 +08:00
import { message, Upload } from 'antd';
2022-05-19 09:46:26 +08:00
import type { UploadChangeParam } from 'antd/es/upload';
import type { RcFile, UploadFile, UploadProps } from 'antd/es/upload/interface';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2016-11-25 15:00:28 +08:00
2022-05-19 09:46:26 +08:00
const getBase64 = (img: RcFile, callback: (url: string) => void) => {
2016-11-25 15:00:28 +08:00
const reader = new FileReader();
2022-05-19 09:46:26 +08:00
reader.addEventListener('load', () => callback(reader.result as string));
2016-11-25 15:00:28 +08:00
reader.readAsDataURL(img);
2022-05-19 09:46:26 +08:00
};
2016-11-25 15:00:28 +08:00
2022-05-19 09:46:26 +08:00
const beforeUpload = (file: RcFile) => {
2019-07-22 19:18:47 +08:00
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
message.error('You can only upload JPG/PNG file!');
2016-11-25 15:00:28 +08:00
}
const isLt2M = file.size / 1024 / 1024 < 2 ;
if (!isLt2M) {
message.error('Image must smaller than 2MB!');
}
2019-07-22 19:18:47 +08:00
return isJpgOrPng & & isLt2M;
2022-05-19 09:46:26 +08:00
};
2016-11-25 15:00:28 +08:00
2022-05-19 09:46:26 +08:00
const App: React.FC = () => {
const [loading, setLoading] = useState(false);
const [imageUrl, setImageUrl] = useState< string > ();
2018-06-27 15:55:04 +08:00
2022-05-19 09:46:26 +08:00
const handleChange: UploadProps['onChange'] = (info: UploadChangeParam< UploadFile > ) => {
2017-10-17 14:43:52 +08:00
if (info.file.status === 'uploading') {
2022-05-19 09:46:26 +08:00
setLoading(true);
2017-10-17 14:43:52 +08:00
return;
}
2016-11-25 15:00:28 +08:00
if (info.file.status === 'done') {
// Get this url from response in real world.
2022-05-19 09:46:26 +08:00
getBase64(info.file.originFileObj as RcFile, url => {
setLoading(false);
setImageUrl(url);
});
2016-11-25 15:00:28 +08:00
}
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2022-05-19 09:46:26 +08:00
const uploadButton = (
< div >
{loading ? < LoadingOutlined / > : < PlusOutlined / > }
< div style = {{ marginTop: 8 } } > Upload< / div >
< / div >
);
return (
< Upload
name="avatar"
listType="picture-card"
className="avatar-uploader"
showUploadList={false}
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
beforeUpload={beforeUpload}
onChange={handleChange}
>
{imageUrl ? < img src = {imageUrl} alt = "avatar" style = {{ width: ' 100 % ' } } / > : uploadButton}
< / Upload >
);
};
2016-11-25 15:00:28 +08:00
2022-05-19 09:46:26 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```