@@ -23,6 +23,7 @@ class ReactImageUploadComponent extends React.Component {
23
23
} ;
24
24
this . inputElement = '' ;
25
25
this . onDropFile = this . onDropFile . bind ( this ) ;
26
+ this . onUploadClick = this . onUploadClick . bind ( this ) ;
26
27
this . triggerFileUpload = this . triggerFileUpload . bind ( this ) ;
27
28
}
28
29
@@ -33,59 +34,72 @@ class ReactImageUploadComponent extends React.Component {
33
34
this . inputElement . click ( ) ;
34
35
}
35
36
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
+ }
42
41
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 = [ ] ;
60
48
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
+ }
68
66
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
+ }
76
69
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 ( ) ;
79
73
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
+ }
89
103
90
104
/*
91
105
Render the upload icon
@@ -197,8 +211,9 @@ class ReactImageUploadComponent extends React.Component {
197
211
type = "file"
198
212
ref = { input => this . inputElement = input }
199
213
name = { this . props . name }
200
- multiple = "multiple"
214
+ multiple = { ! this . props . singleImage }
201
215
onChange = { this . onDropFile }
216
+ onClick = { this . onUploadClick }
202
217
accept = { this . props . accept }
203
218
/>
204
219
{ this . props . withPreview ? this . renderPreview ( ) : null }
0 commit comments