Skip to content
This repository was archived by the owner on Jun 5, 2022. It is now read-only.

Commit f847f0a

Browse files
author
Jake Hartnell
committed
Merge #54
2 parents 5fb5a1e + bde1ceb commit f847f0a

File tree

2 files changed

+65
-49
lines changed

2 files changed

+65
-49
lines changed

src/component/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class App extends React.Component {
6565
| fileTypeError | String | " is not supported file extension" | Label for file extension error message. |
6666
| errorClass | String | - | Class for error messages |
6767
| errorStyle | Object | - | Inline styles for errors |
68+
| singleImage | Boolean | false | If true, only a single image can be selected |
6869

6970
### License
7071
MIT

src/component/index.js

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ReactImageUploadComponent extends React.Component {
2323
};
2424
this.inputElement = '';
2525
this.onDropFile = this.onDropFile.bind(this);
26+
this.onUploadClick = this.onUploadClick.bind(this);
2627
this.triggerFileUpload = this.triggerFileUpload.bind(this);
2728
}
2829

@@ -33,59 +34,72 @@ class ReactImageUploadComponent extends React.Component {
3334
this.inputElement.click();
3435
}
3536

36-
/*
37-
Handle file validation
38-
*/
39-
onDropFile(e) {
40-
const files = e.target.files;
41-
const _this = this;
37+
onUploadClick(e) {
38+
// Fixes https://github.com/JakeHartnell/react-images-upload/issues/55
39+
e.target.value = null;
40+
}
4241

43-
// Iterate over all uploaded files
44-
for (let i = 0; i < files.length; i++) {
45-
let f = files[i];
46-
// Check for file extension
47-
if (!this.hasExtension(f.name)) {
48-
const newArray = _this.state.notAcceptedFileType.slice();
49-
newArray.push(f.name);
50-
_this.setState({notAcceptedFileType: newArray});
51-
continue;
52-
}
53-
// Check for file size
54-
if(f.size > this.props.maxFileSize) {
55-
const newArray = _this.state.notAcceptedFileSize.slice();
56-
newArray.push(f.name);
57-
_this.setState({notAcceptedFileSize: newArray});
58-
continue;
59-
}
42+
/*
43+
Handle file validation
44+
*/
45+
onDropFile(e) {
46+
const files = e.target.files;
47+
const allFilePromises = [];
6048

61-
const reader = new FileReader();
62-
// Read the image via FileReader API and save image result in state.
63-
reader.onload = (function () {
64-
return function (e) {
65-
// Add the file name to the data URL
66-
let dataURL = e.target.result;
67-
dataURL = dataURL.replace(";base64", `;name=${f.name};base64`);
49+
// Iterate over all uploaded files
50+
for (let i = 0; i < files.length; i++) {
51+
let f = files[i];
52+
// Check for file extension
53+
if (!this.hasExtension(f.name)) {
54+
const newArray = this.state.notAcceptedFileType.slice();
55+
newArray.push(f.name);
56+
this.setState({notAcceptedFileType: newArray});
57+
continue;
58+
}
59+
// Check for file size
60+
if(f.size > this.props.maxFileSize) {
61+
const newArray = this.state.notAcceptedFileSize.slice();
62+
newArray.push(f.name);
63+
this.setState({notAcceptedFileSize: newArray});
64+
continue;
65+
}
6866

69-
if (_this.props.singleImage === true) {
70-
_this.setState({pictures: [dataURL], files: [f]}, () => {
71-
_this.props.onChange(_this.state.files, _this.state.pictures);
72-
});
73-
} else if (_this.state.pictures.indexOf(dataURL) === -1) {
74-
const newArray = _this.state.pictures.slice();
75-
newArray.push(dataURL);
67+
allFilePromises.push(this.readFile(f));
68+
}
7669

77-
const newFiles = _this.state.files.slice();
78-
newFiles.push(f);
70+
Promise.all(allFilePromises).then(newFilesData => {
71+
const dataURLs = this.state.pictures.slice();
72+
const files = this.state.files.slice();
7973

80-
_this.setState({pictures: newArray, files: newFiles}, () => {
81-
_this.props.onChange(_this.state.files, _this.state.pictures);
82-
});
83-
}
84-
};
85-
})(f);
86-
reader.readAsDataURL(f);
87-
}
88-
}
74+
newFilesData.forEach(newFileData => {
75+
dataURLs.push(newFileData.dataURL);
76+
files.push(newFileData.file);
77+
});
78+
79+
this.setState({pictures: dataURLs, files: files}, () => {
80+
this.props.onChange(this.state.files, this.state.pictures);
81+
});
82+
});
83+
}
84+
85+
/*
86+
Read a file and return a promise that when resolved gives the file itself and the data URL
87+
*/
88+
readFile(file) {
89+
return new Promise((resolve, reject) => {
90+
const reader = new FileReader();
91+
92+
// Read the image via FileReader API and save image result in state.
93+
reader.onload = function (e) {
94+
// Add the file name to the data URL
95+
let dataURL = e.target.result;
96+
dataURL = dataURL.replace(";base64", `;name=${file.name};base64`);
97+
resolve({file, dataURL});
98+
};
99+
100+
reader.readAsDataURL(file);
101+
});
102+
}
89103

90104
/*
91105
Render the upload icon
@@ -197,8 +211,9 @@ class ReactImageUploadComponent extends React.Component {
197211
type="file"
198212
ref={input => this.inputElement = input}
199213
name={this.props.name}
200-
multiple="multiple"
214+
multiple={!this.props.singleImage}
201215
onChange={this.onDropFile}
216+
onClick={this.onUploadClick}
202217
accept={this.props.accept}
203218
/>
204219
{ this.props.withPreview ? this.renderPreview() : null }

0 commit comments

Comments
 (0)