-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadFile.js
More file actions
170 lines (165 loc) · 5.07 KB
/
UploadFile.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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Upload, Icon, Modal,Button,Input,notification} from 'antd'
import styles from './UploadFile.less'
const getFileList = (files) => {
if (Array.isArray(files)) {
return files.map((item, key) => {
const urlArr = 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) {
const filesArr = files.split('/')
return [{ uid: -1, url: files, name: filesArr[filesArr.length - 1], status: 'done' }]
}
return ''
}
function renderAccecpt (accept) {
if (!accept) {
return null
}
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}`
}
class UploadFiles extends Component {
static propTypes = {
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;
const ishttp=weburl.indexOf('http')>-1;
const isimg =weburl.indexOf('.jpg')>-1||weburl.indexOf('.png')||weburl.indexOf('.jepg')>-1;
if(!ishttp||!isimg){
return notification.error({message: '添加失败',description:"非法链接"});
}
let newFiles = [];
const obj={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
const renderFiles = (fileList) => {
const newFiles = fileList.map((file) => {
return file.response ? {id:file.response.data.id,url:file.response.data.url,key:file.response.data.key} : file
})
if (multiple === 1) {
return newFiles[0]
}
return newFiles
}
//post url
let actionUrl = `${API_URL}/file`
if (path) {
actionUrl += `&path=${path}`
}
const uploadProps = {
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: () => {
return true
},
onChange: ({ file, fileList }) => {
this.setState({ files: fileList })
if (file.percent === 100 && file.status === 'done') {
onUpload(renderFiles(fileList, 1))
}
},
onRemove: (file) => {
let fileKey;
const {response,key} = file;
fileKey=key;
if(response){
fileKey=response.data.key;
}
if (disabled) {
return false
}
const fileList = this.state.files.filter(item => item.uid !== file.uid)
onUpload(renderFiles(fileList, 0))
onRemove(fileKey);
return true
},
}
const modalProps = {
visible: previewVisible,
footer: null,
onCancel: () => this.setState({ previewVisible: false }),
}
const modalPropsPic = {
title:'添加网络图片',
visible: true,
onCancel: () => this.setState({webpicVisible:false}),
onOk:()=>this.useWebPic()
}
const uploadButton = (
<div>
<Icon type="plus" />
<div className="ant-upload-text">本地上传</div>
</div>
)
return (
<div className="clearfix">
<Upload {...uploadProps}>
{multiple === true ? uploadButton : (files.length < multiple && uploadButton)}
</Upload>
{showWeb&&<Button icon="plus" onClick={this.showWebPic}>网络图片</Button>}
<Modal {...modalProps}>
<img className={styles.previewImage} src={previewImage} />
</Modal>
{webpicVisible&&<Modal {...modalPropsPic} >
<Input placeholder="输入图片链接..." onChange={this.picInputChange} style={{width:'100%'}}/>
</Modal>}
</div>
)
}
}
export default UploadFiles