File tree Expand file tree Collapse file tree 3 files changed +18
-12
lines changed Expand file tree Collapse file tree 3 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ React.render(<Upload />, container);
61
61
| action| string | ; function(file): string | ; Promise< ; string> ; | | form action url |
62
62
| method | string | post | request method |
63
63
| directory| boolean | false | support upload whole directory |
64
- | data| object/function(file) | | other data object to post or a function which returns a data object |
64
+ | data| object/function(file) | | other data object to post or a function which returns a data object(a promise object which resolve a data object) |
65
65
| headers| object | {} | http headers to post, available in modern browsers |
66
66
| accept | string | | input accept attribute |
67
67
| multiple | boolean | false | only support ie10+|
Original file line number Diff line number Diff line change @@ -125,31 +125,29 @@ class AjaxUploader extends Component {
125
125
return ;
126
126
}
127
127
const { props } = this ;
128
- let { data } = props ;
129
128
const {
130
129
onStart,
131
130
onProgress,
132
131
transformFile = ( originFile ) => originFile ,
133
132
} = props ;
134
133
135
134
new Promise ( resolve => {
136
- const { action } = props ;
135
+ let { data , action } = props ;
137
136
if ( typeof action === 'function' ) {
138
- return resolve ( action ( file ) ) ;
137
+ action = action ( file ) ;
139
138
}
140
- resolve ( action ) ;
141
- } ) . then ( action => {
139
+ if ( typeof data === 'function' ) {
140
+ data = data ( file ) ;
141
+ }
142
+ resolve ( Promise . all ( [ action , data ] ) ) ;
143
+ } ) . then ( ( [ action , data ] ) => {
142
144
const { uid } = file ;
143
145
const request = props . customRequest || defaultRequest ;
144
146
const transform = Promise . resolve ( transformFile ( file ) ) . catch ( e => {
145
147
console . error ( e ) ; // eslint-disable-line no-console
146
148
} ) ;
147
149
148
150
transform . then ( ( transformedFile ) => {
149
- if ( typeof data === 'function' ) {
150
- data = data ( file ) ;
151
- }
152
-
153
151
const requestOption = {
154
152
action,
155
153
filename : props . name ,
Original file line number Diff line number Diff line change @@ -338,15 +338,22 @@ describe('uploader', () => {
338
338
} ) ;
339
339
} ) ;
340
340
341
- it ( 'support action is function returns Promise' , done => {
341
+ it ( 'support action and data is function returns Promise' , done => {
342
342
const action = ( ) => {
343
343
return new Promise ( resolve => {
344
344
setTimeout ( ( ) => {
345
345
resolve ( '/upload.do' ) ;
346
346
} , 1000 ) ;
347
347
} ) ;
348
348
} ;
349
- ReactDOM . render ( < Uploader action = { action } /> , node , function init ( ) {
349
+ const data = ( ) => {
350
+ return new Promise ( resolve => {
351
+ setTimeout ( ( ) => {
352
+ resolve ( { field1 : 'a' } ) ;
353
+ } , 1000 ) ;
354
+ } ) ;
355
+ } ;
356
+ ReactDOM . render ( < Uploader data = { data } action = { action } /> , node , function init ( ) {
350
357
uploader = this ;
351
358
const input = TestUtils . findRenderedDOMComponentWithTag ( uploader , 'input' ) ;
352
359
const files = [
@@ -365,6 +372,7 @@ describe('uploader', () => {
365
372
console . log ( requests ) ;
366
373
expect ( requests . length ) . to . be ( 1 ) ;
367
374
expect ( requests [ 0 ] . url ) . to . be ( '/upload.do' ) ;
375
+ expect ( requests [ 0 ] . requestBody . get ( 'field1' ) ) . to . be ( 'a' ) ;
368
376
done ( ) ;
369
377
} , 1000 ) ;
370
378
} , 100 ) ;
You can’t perform that action at this time.
0 commit comments