import
React, {
Component }
from
'react';
import {
Link,
IndexLink,
hashHistory}
from
"react-router";
import {
Upload,
message,}
from
'antd';
export
default
class
Upload
extends
Component {
constructor(
props){
super(
props)
this.
state={
fileList : [],
}
}
uplodChange(
fileList) {
this.
setState({
fileList }.
fileList);
}
beforeUpload(
file) {
const
isJPG =
file.
type ===
'image/jpeg';
const
isPNG =
file.
type ===
'image/png';
const
isGIF =
file.
type ===
'image/gif';
if (!
isJPG && !
isPNG && !
isGIF) {
message.
error(
'You can upload jpg/png/gif file!');
this.
fileList = []
}
const
isLt2M =
file.
size /
1024 /
1024 <
2;
if (!
isLt2M) {
message.
error(
'Image must smaller than 2MB!');
this.
fileList = []
}
return
isJPG &&
isPNG &&
isGIF &&
isLt2M ; //文件类型判断 只能是png,gif,jpeg,类型大小不能超过2MB
}
onRemove(){
return
false; // Upload 删除回调 false 阻止删除 true正常删除
}
render(){
const {
fileList} =
this.
state
const
uploadFile = {
name:
"autograph",
action:
'',
//图片上传接口
listType:
"picture-card",
defaultFileList: [...
fileList],
className :
"upload-list-inline",
beforeUpload :
this.
beforeUpload,
onChange:
this.
uplodChange.
bind(
this),
onRemove :this.onRemove,
};
return(
<
Upload
fileList=
{
fileList
}
{...
uploadFile
}
>
{
fileList.
length >=
1 ?
null :
<
span
className=
"user_add"
>
<
Icon
type=
"plus"
/>
</
span
>
}
</
Upload
>
)
}
}