@@ -451,7 +451,7 @@ function QiniuJsSDK() {
451451 errorObj = that . parseJSON ( errorObj . error ) ;
452452 errorText = errorObj . error || 'file exists' ;
453453 } catch ( e ) {
454- // console.log(e );
454+ throw ( 'invalid json format' ) ;
455455 }
456456 break ;
457457 case 631 :
@@ -493,6 +493,42 @@ function QiniuJsSDK() {
493493
494494 uploader . bind ( 'FileUploaded' , ( function ( _FileUploaded_Handler ) {
495495 return function ( up , file , info ) {
496+
497+ var last_step = function ( up , file , info ) {
498+ if ( op . downtoken_url ) {
499+ var ajax_downtoken = that . createAjax ( ) ;
500+ ajax_downtoken . open ( 'POST' , op . downtoken_url , true ) ;
501+ ajax_downtoken . setRequestHeader ( 'Content-type' , 'application/x-www-form-urlencoded' ) ;
502+ ajax_downtoken . onreadystatechange = function ( ) {
503+ if ( ajax_downtoken . readyState === 4 ) {
504+ if ( ajax_downtoken . status === 200 ) {
505+ var res_downtoken ;
506+ try {
507+ res_downtoken = that . parseJSON ( ajax_downtoken . responseText ) ;
508+ } catch ( e ) {
509+ throw ( 'invalid json format' ) ;
510+ }
511+ var info_extended = { } ;
512+ plupload . extend ( info_extended , that . parseJSON ( info ) , res_downtoken ) ;
513+ if ( _FileUploaded_Handler ) {
514+ _FileUploaded_Handler ( up , file , JSON . stringify ( info_extended ) ) ;
515+ }
516+ } else {
517+ uploader . trigger ( 'Error' , {
518+ status : ajax_downtoken . status ,
519+ response : ajax_downtoken . responseText ,
520+ file : file ,
521+ code : plupload . HTTP_ERROR
522+ } ) ;
523+ }
524+ }
525+ } ;
526+ ajax_downtoken . send ( 'key=' + that . parseJSON ( info ) . key + '&domain=' + op . domain ) ;
527+ } else if ( _FileUploaded_Handler ) {
528+ _FileUploaded_Handler ( up , file , info ) ;
529+ }
530+ } ;
531+
496532 var res = that . parseJSON ( info . response ) ;
497533 ctx = ctx ? ctx : res . ctx ;
498534 if ( ctx ) {
@@ -523,15 +559,11 @@ function QiniuJsSDK() {
523559 ajax . open ( 'POST' , url , true ) ;
524560 ajax . setRequestHeader ( 'Content-Type' , 'text/plain;charset=UTF-8' ) ;
525561 ajax . setRequestHeader ( 'Authorization' , 'UpToken ' + that . token ) ;
526- ajax . send ( ctx ) ;
527562 ajax . onreadystatechange = function ( ) {
528563 if ( ajax . readyState === 4 ) {
529564 if ( ajax . status === 200 ) {
530565 var info = ajax . responseText ;
531-
532- if ( _FileUploaded_Handler ) {
533- _FileUploaded_Handler ( up , file , info ) ;
534- }
566+ last_step ( up , file , info ) ;
535567
536568 } else {
537569 uploader . trigger ( 'Error' , {
@@ -543,10 +575,9 @@ function QiniuJsSDK() {
543575 }
544576 }
545577 } ;
578+ ajax . send ( ctx ) ;
546579 } else {
547- if ( _FileUploaded_Handler ) {
548- _FileUploaded_Handler ( up , file , info . response ) ;
549- }
580+ last_step ( up , file , info . response ) ;
550581 }
551582
552583 } ;
0 commit comments