2020-08-07 17:07:54 +08:00
---
order: 7.1
title:
zh-CN: 只上传 png 图片
en-US: Upload png file only
---
## zh-CN
2021-02-26 15:18:37 +08:00
`beforeUpload` 返回 `false` 或 `Promise.reject` 时,只用于拦截上传行为,不会阻止文件进入上传列表([原因](https://github.com/ant-design/ant-design/issues/15561#issuecomment-475108235))。如果需要阻止列表展现,可以通过返回 `Upload.LIST_IGNORE` 实现。
2020-08-07 17:07:54 +08:00
## en-US
2021-02-26 15:18:37 +08:00
`beforeUpload` only prevent upload behavior when return false or reject promise, the prevented file would still show in file list. Here is the example you can keep prevented files out of list by return `UPLOAD.LIST_IGNORE` .
2020-08-07 17:07:54 +08:00
2022-05-19 09:46:26 +08:00
```tsx
2020-08-07 17:07:54 +08:00
import { UploadOutlined } from '@ant-design/icons';
2022-05-19 09:46:26 +08:00
import type { UploadProps } from 'antd';
2022-05-23 14:37:16 +08:00
import { Button, message, Upload } from 'antd';
import React from 'react';
2020-08-07 17:07:54 +08:00
2022-05-19 09:46:26 +08:00
const props: UploadProps = {
beforeUpload: file => {
const isPNG = file.type === 'image/png';
if (!isPNG) {
message.error(`${file.name} is not a png file`);
}
return isPNG || Upload.LIST_IGNORE;
},
onChange: info => {
console.log(info.fileList);
},
2020-08-07 17:07:54 +08:00
};
2022-05-19 09:46:26 +08:00
const App: React.FC = () => (
< Upload { . . . props } >
< Button icon = {<UploadOutlined / > }>Upload png only< / Button >
< / Upload >
);
2022-04-20 09:48:26 +08:00
export default App;
2020-08-07 17:07:54 +08:00
```