Skip to content

Commit

Permalink
fix(ajaxuploader): support ie10 upload the same file twice
Browse files Browse the repository at this point in the history
close #37
  • Loading branch information
翰文 committed Jun 2, 2016
1 parent 9b5b6e1 commit fb6c102
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/AjaxUploader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@ import request from './request';
import React, {PropTypes} from 'react';
import uid from './uid';

const AjaxUploader = React.createClass({
propTypes: {
multiple: PropTypes.bool,
onStart: PropTypes.func,
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func,
]),
headers: PropTypes.object,
beforeUpload: PropTypes.func,
withCredentials: PropTypes.bool,
},
class AjaxUploader extends React.Component {

constructor(props) {
super(props);
['onChange', 'onClick', 'onKeyDown', 'onFileDrop'].map(fn => this[fn] = this[fn].bind(this));
this.state = { inputKey: uid() };
}

onChange(e) {
const files = e.target.files;
this.uploadFiles(files);
},
}

onClick() {
const el = this.refs.file;
if (!el) {
return;
}
el.click();
el.value = '';
},
}

onKeyDown(e) {
if (e.key === 'Enter') {
this.onClick();
}
},
}

onFileDrop(e) {
if (e.type === 'dragover') {
Expand All @@ -44,7 +38,7 @@ const AjaxUploader = React.createClass({
this.uploadFiles(files);

e.preventDefault();
},
}

uploadFiles(files) {
const len = files.length;
Expand All @@ -60,7 +54,7 @@ const AjaxUploader = React.createClass({
this.props.onStart(Array.prototype.slice.call(files)[0]);
}
}
},
}

upload(file) {
const props = this.props;
Expand All @@ -76,7 +70,7 @@ const AjaxUploader = React.createClass({
} else if (before !== false) {
this.post(file);
}
},
}

post(file) {
const props = this.props;
Expand All @@ -97,12 +91,20 @@ const AjaxUploader = React.createClass({
},
onSuccess: ret => {
props.onSuccess(ret, file);
this._reset();
},
onError: (err, ret) => {
props.onError(err, ret, file);
this._reset();
},
});
},
}

_reset() {
// reset dom, so can reset element value
// fix https://github.com/react-component/upload/issues/37
this.setState({ inputKey: uid() });
}

render() {
const hidden = {display: 'none'};
Expand All @@ -118,14 +120,27 @@ const AjaxUploader = React.createClass({
>
<input type="file"
ref="file"
key={this.state.inputKey}
style={hidden}
accept={props.accept}
multiple={this.props.multiple}
onChange={this.onChange}/>
{props.children}
</span>
);
},
});
}
}

AjaxUploader.propTypes = {
multiple: PropTypes.bool,
onStart: PropTypes.func,
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func,
]),
headers: PropTypes.object,
beforeUpload: PropTypes.func,
withCredentials: PropTypes.bool,
};

export default AjaxUploader;

0 comments on commit fb6c102

Please sign in to comment.