-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
171 lines (148 loc) · 5.16 KB
/
index.js
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
171
/*global URL */
'use strict';
var React = require('react');
var ReactDOM = require('react-dom')
var request = require('superagent-bluebird-promise');
var isFunction = function (fn) {
var getType = {};
return fn && getType.toString.call(fn) === '[object Function]';
};
function formatMaxSize(size){
size=size.toString().toUpperCase();
var bsize,m=size.indexOf('M'),k=size.indexOf('K');
if(m > -1){
bsize = parseFloat(size.slice(0, m)) * 1024 * 1024
}else if(k > -1){
bsize = parseFloat(size.slice(0, k)) * 1024
}else{
bsize = parseFloat(size)
}
return Math.abs(bsize)
}
var ReactQiniu = React.createClass({
// based on https://github.com/paramaggarwal/react-dropzone
propTypes: {
onDrop: React.PropTypes.func.isRequired,
token: React.PropTypes.string.isRequired,
// called before upload to set callback to files
onUpload: React.PropTypes.func,
size: React.PropTypes.number,
style: React.PropTypes.object,
supportClick: React.PropTypes.bool,
accept: React.PropTypes.string,
multiple: React.PropTypes.bool,
// Qiniu
uploadUrl: React.PropTypes.string,
prefix: React.PropTypes.string,
//props to check File Size before upload.example:'2Mb','30k'...
maxSize:React.PropTypes.string,
},
getDefaultProps: function() {
var uploadUrl = 'http://upload.qiniu.com'
if (window.location.protocol === 'https:') {
uploadUrl = 'https://up.qbox.me/'
}
return {
supportClick: true,
multiple: true,
uploadUrl: uploadUrl
};
},
getInitialState: function() {
return {
isDragActive: false
};
},
onDragLeave: function(e) {
this.setState({
isDragActive: false
});
},
onDragOver: function(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
this.setState({
isDragActive: true
});
},
onDrop: function(e) {
e.preventDefault();
this.setState({
isDragActive: false
});
var files;
if (e.dataTransfer) {
files = e.dataTransfer.files;
} else if (e.target) {
files = e.target.files;
}
var maxFiles = (this.props.multiple) ? files.length : 1;
if (this.props.onUpload) {
files = Array.prototype.slice.call(files, 0, maxFiles);
this.props.onUpload(files, e);
}
var maxSizeLimit=formatMaxSize(this.props.maxSize)
for (var i = 0; i < maxFiles; i++) {
if( maxSizeLimit && files[i].size > maxSizeLimit){
console.trace && console.trace(new Error('文件大小错误!'))
this.props.onError && this.props.onError({
coed:1,
message:'上传的文件大小超出了限制:' + this.props.maxSize
})
}else{
files[i].preview = URL.createObjectURL(files[i]);
files[i].request = this.upload(files[i]);
files[i].uploadPromise = files[i].request.promise();
}
}
if (this.props.onDrop) {
files = Array.prototype.slice.call(files, 0, maxFiles);
this.props.onDrop(files, e);
}
},
onClick: function () {
if (this.props.supportClick) {
this.open();
}
},
open: function() {
var fileInput = ReactDOM.findDOMNode(this.refs.fileInput);
fileInput.value = null;
fileInput.click();
},
upload: function(file) {
if (!file || file.size === 0) return null;
var key = file.preview.split('/').pop() + '.' + file.name.split('.').pop();
if (this.props.prefix) {
key = this.props.prefix + key;
}
var r = request
.post(this.props.uploadUrl)
.field('key', key)
.field('token', this.props.token)
.field('x:filename', file.name)
.field('x:size', file.size)
.attach('file', file, file.name)
.set('Accept', 'application/json');
if (isFunction(file.onprogress)) { r.on('progress', file.onprogress); }
return r;
},
render: function() {
var className = this.props.className || 'dropzone';
if (this.state.isDragActive) {
className += ' active';
}
var style = this.props.style || {
width: this.props.size || 100,
height: this.props.size || 100,
borderStyle: this.state.isDragActive ? 'solid' : 'dashed'
};
return (
React.createElement('div', {className: className, style: style, onClick: this.onClick, onDragLeave: this.onDragLeave, onDragOver: this.onDragOver, onDrop: this.onDrop},
React.createElement('input', {style: {display: 'none'}, type: 'file', multiple: this.props.multiple, ref: 'fileInput', onChange: this.onDrop, accept: this.props.accept}),
this.props.children
)
);
}
});
module.exports = ReactQiniu;