- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadFile.js
More file actions
Latest commit
170 lines (165 loc) · 5.07 KB
/
Copy pathUploadFile.js
File metadata and controls
170 lines (165 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
importReact,{Component}from'react'
importPropTypesfrom'prop-types'
import{Upload,Icon,Modal,Button,Input,notification}from'antd'
importstylesfrom'./UploadFile.less'
constgetFileList=(files)=>{
if(Array.isArray(files)){
returnfiles.map((item,key)=>{
consturlArr=item.url.split('/')
return{url: item.url,id: item.id,uid: key,key:item.key,name: urlArr[urlArr.length-1],status: 'done'}
})
}
if(files&&!!files.length){
constfilesArr=files.split('/')
return[{uid: -1,url: files,name: filesArr[filesArr.length-1],status: 'done'}]
}
return''
}
functionrenderAccecpt(accept){
if(!accept){
returnnull
}
if(['image','video','audio'].find(ext=>ext===accept)){
return`${accept}/*`
}
if(accept==='zip'){
return'application/zip,application/x-zip,application/x-zip-compressed'
}
return`.${accept}`
}
classUploadFilesextendsComponent{
staticpropTypes={
files: PropTypes.oneOfType([PropTypes.array,PropTypes.string]),
onUpload: PropTypes.func.isRequired,
multiple: PropTypes.oneOfType([PropTypes.bool,PropTypes.number]),
disabled: PropTypes.bool,
path: PropTypes.string,
accept: PropTypes.string,
}
constructor(props){
super(props)
this.state={
previewVisible: false,
previewImage: '',
files: getFileList(props.files),
webpicVisible:false,
weburl:''
}
}
componentWillReceiveProps(nextProps){
if(Array.isArray(this.props.files)&&!this.props.files.length&&!!nextProps.files.length){
this.setState({files: getFileList(nextProps.files)})
}
}
showWebPic=()=>{
this.setState({webpicVisible:true})
}
picInputChange=(e)=>{
this.setState({weburl:e.target.value})
}
useWebPic=()=>{
const{weburl,files}=this.state;
constishttp=weburl.indexOf('http')>-1;
constisimg=weburl.indexOf('.jpg')>-1||weburl.indexOf('.png')||weburl.indexOf('.jepg')>-1;
if(!ishttp||!isimg){
returnnotification.error({message: '添加失败',description:"非法链接"});
}
letnewFiles=[];
constobj={uid:files.length+1,id:files.length+1,url:weburl,key:weburl.split('.com')[1],status:'done'};
newFiles.push(obj);
this.props.onUpload(obj);
this.setState({files:newFiles,webpicVisible:false})
}
render(){
const{ previewVisible, webpicVisible,previewImage,files}=this.state
const{ multiple =1,showWeb,onUpload,onRemove, disabled,data,path, accept }=this.props
constrenderFiles=(fileList)=>{
constnewFiles=fileList.map((file)=>{
returnfile.response ? {id:file.response.data.id,url:file.response.data.url,key:file.response.data.key} : file
})
if(multiple===1){
returnnewFiles[0]
}
returnnewFiles
}
//post url
letactionUrl=`${API_URL}/file`
if(path){
actionUrl+=`&path=${path}`
}
constuploadProps={
accept: renderAccecpt(accept),
action: actionUrl,
headers: {
'X-Requested-With': null,
},
data:data,
disabled,
listType: 'picture-card',
fileList: files,
multiple: multiple===true,
onPreview: (file)=>{
this.setState({
previewImage: file.url||file.thumbUrl,
previewVisible: true,
})
},
beforeUpload: ()=>{
returntrue
},
onChange: ({ file, fileList })=>{
this.setState({files: fileList})
if(file.percent===100&&file.status==='done'){
onUpload(renderFiles(fileList,1))
}
},
onRemove: (file)=>{
letfileKey;
const{response,key}=file;
fileKey=key;
if(response){
fileKey=response.data.key;
}
if(disabled){
returnfalse
}
constfileList=this.state.files.filter(item=>item.uid!==file.uid)
onUpload(renderFiles(fileList,0))
onRemove(fileKey);
returntrue
},
}
constmodalProps={
visible: previewVisible,
footer: null,
onCancel: ()=>this.setState({previewVisible: false}),
}
constmodalPropsPic={
title:'添加网络图片',
visible: true,
onCancel: ()=>this.setState({webpicVisible:false}),
onOk:()=>this.useWebPic()
}
constuploadButton=(
<div>
<Icontype="plus"/>
<divclassName="ant-upload-text">本地上传</div>
</div>
)
return(
<divclassName="clearfix">
<Upload{...uploadProps}>
{multiple===true ? uploadButton : (files.length<multiple&&uploadButton)}
</Upload>
{showWeb&&<Buttonicon="plus"onClick={this.showWebPic}>网络图片</Button>}
<Modal{...modalProps}>
<imgclassName={styles.previewImage}src={previewImage}/>
</Modal>
{webpicVisible&&<Modal{...modalPropsPic}>
<Inputplaceholder="输入图片链接..."onChange={this.picInputChange}style={{width:'100%'}}/>
</Modal>}
</div>
)
}
}
exportdefaultUploadFiles