2016-03-31 09:40:55 +08:00
|
|
|
|
---
|
|
|
|
|
order: 7
|
|
|
|
|
title: 限制用户上传的文件
|
|
|
|
|
---
|
2015-12-28 14:16:34 +08:00
|
|
|
|
|
|
|
|
|
可以通过 `beforeUpload` 在文件上传之前进行干预,如限制用户只能上传 JPG 文件。
|
|
|
|
|
|
2016-05-25 17:32:41 +08:00
|
|
|
|
也支持异步检查,`beforeUpload` 的返回值可以是一个 Promise:[示例](http://react-component.github.io/upload/examples/beforeUpload.html)。
|
|
|
|
|
|
2015-12-28 14:16:34 +08:00
|
|
|
|
````jsx
|
|
|
|
|
import { Upload, Button, Icon, message } from 'antd';
|
|
|
|
|
|
|
|
|
|
const props = {
|
|
|
|
|
action: '/upload.do',
|
2016-02-17 15:57:33 +08:00
|
|
|
|
beforeUpload(file) {
|
2015-12-28 14:16:34 +08:00
|
|
|
|
const isJPG = file.type === 'image/jpeg';
|
|
|
|
|
if (!isJPG) {
|
|
|
|
|
message.error('只能上传 JPG 文件哦!');
|
|
|
|
|
}
|
|
|
|
|
return isJPG;
|
2016-05-11 09:32:33 +08:00
|
|
|
|
},
|
2015-12-28 14:16:34 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
|
<Upload {...props}>
|
|
|
|
|
<Button type="ghost">
|
|
|
|
|
<Icon type="upload" /> 点击上传
|
|
|
|
|
</Button>
|
|
|
|
|
</Upload>
|
2015-12-29 12:08:58 +08:00
|
|
|
|
, mountNode);
|
2015-12-28 14:16:34 +08:00
|
|
|
|
````
|