forked from odysseyscience/react-s3-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactS3Uploader.js
79 lines (67 loc) · 2.38 KB
/
ReactS3Uploader.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
"use strict";
var React = require('react'),
S3Upload = require('./s3upload.js'),
objectAssign = require('object-assign');
var ReactS3Uploader = React.createClass({
propTypes: {
signingUrl: React.PropTypes.string.isRequired,
onProgress: React.PropTypes.func,
onFinish: React.PropTypes.func,
onError: React.PropTypes.func,
signingUrlHeaders: React.PropTypes.object,
signingUrlQueryParams: React.PropTypes.object,
uploadRequestHeaders: React.PropTypes.object,
contentDisposition: React.PropTypes.string
},
getDefaultProps: function() {
return {
onProgress: function(percent, message) {
console.log('Upload progress: ' + percent + '% ' + message);
},
onFinish: function(signResult) {
console.log("Upload finished: " + signResult.publicUrl)
},
onError: function(message) {
console.log("Upload error: " + message);
}
};
},
uploadFile: function() {
new S3Upload({
fileElement: findDOMNode(this),
signingUrl: this.props.signingUrl,
onProgress: this.props.onProgress,
onFinishS3Put: this.props.onFinish,
onError: this.props.onError,
signingUrlHeaders: this.props.signingUrlHeaders,
signingUrlQueryParams: this.props.signingUrlQueryParams,
uploadRequestHeaders: this.props.uploadRequestHeaders,
contentDisposition: this.props.contentDisposition
});
},
clear: function() {
clearInputFile(findDOMNode(this));
},
render: function() {
return React.DOM.input(objectAssign({}, this.props, {type: 'file', onChange: this.uploadFile}));
}
});
function findDOMNode(cmp) {
return React.findDOMNode ? React.findDOMNode(cmp) : cmp.getDOMNode();
}
// http://stackoverflow.com/a/24608023/194065
function clearInputFile(f){
if(f.value){
try{
f.value = ''; //for IE11, latest Chrome/Firefox/Opera...
}catch(err){ }
if(f.value){ //for IE5 ~ IE10
var form = document.createElement('form'),
parentNode = f.parentNode, ref = f.nextSibling;
form.appendChild(f);
form.reset();
parentNode.insertBefore(f,ref);
}
}
}
module.exports = ReactS3Uploader;