@@ -2,6 +2,7 @@ import { CancellablePromise, Http } from '../http/http';
2
2
import { HttpError , HttpRequestOptions , ProgressEvent } from '../http/http-request-common' ;
3
3
import { FileManager } from '../file/file' ;
4
4
import { knownFolders , path as filePath , File as fsFile , Utils } from '@nativescript/core' ;
5
+ import { Helpers } from '@nativescript/canvas/helpers' ;
5
6
6
7
enum XMLHttpRequestResponseType {
7
8
empty = '' ,
@@ -474,6 +475,133 @@ export class TNSXMLHttpRequest {
474
475
return ;
475
476
}
476
477
478
+ if ( ! path ) {
479
+ const startEvent = new ProgressEvent ( 'loadstart' , this . _lastProgress ) ;
480
+
481
+ if ( this . onnloadstart ) {
482
+ this . onnloadstart ( startEvent ) ;
483
+ }
484
+
485
+ this . emitEvent ( 'loadstart' , startEvent ) ;
486
+
487
+ this . _updateReadyStateChange ( this . LOADING ) ;
488
+
489
+ if ( this . _request . url . startsWith ( 'data:' ) ) {
490
+ const split = this . _request . url . split ( ',' ) ;
491
+ const mime = split [ 0 ] ;
492
+ const base64result = split [ 1 ] ;
493
+ if ( mime && base64result ) {
494
+ Helpers . base64DecodeAsync ( base64result )
495
+ . then ( ( result ) => {
496
+ this . _headers [ 'Content-Type' ] = mime ;
497
+ const data = result [ 1 ] ;
498
+
499
+ this . _response = data ?? '' ;
500
+ this . _responseText = data [ 0 ] ?? '' ;
501
+
502
+ this . _status = 200 ;
503
+ this . _responseURL = this . _request . url ;
504
+
505
+ const size = data ?. byteLength ?? 0 ;
506
+ this . _lastProgress = {
507
+ lengthComputable : true ,
508
+ loaded : size ,
509
+ total : size ,
510
+ target : this ,
511
+ } ;
512
+
513
+ const progressEvent = new ProgressEvent ( 'progress' , this . _lastProgress ) ;
514
+ if ( this . onprogress ) {
515
+ this . onprogress ( progressEvent ) ;
516
+ }
517
+ this . emitEvent ( 'progress' , progressEvent ) ;
518
+
519
+ this . _addToStringOnResponse ( ) ;
520
+
521
+ const loadEvent = new ProgressEvent ( 'load' , this . _lastProgress ) ;
522
+
523
+ if ( this . onnload ) {
524
+ this . onnload ( loadEvent ) ;
525
+ }
526
+
527
+ this . emitEvent ( 'load' , loadEvent ) ;
528
+
529
+ const loadendEvent = new ProgressEvent ( 'loadend' , this . _lastProgress ) ;
530
+
531
+ if ( this . onnloadend ) {
532
+ this . onnloadend ( loadendEvent ) ;
533
+ }
534
+
535
+ this . emitEvent ( 'loadend' , loadendEvent ) ;
536
+
537
+ this . _updateReadyStateChange ( this . DONE ) ;
538
+ } )
539
+ . catch ( ( _ ) => {
540
+ const errorEvent = new ProgressEvent ( 'error' , this . _lastProgress ) ;
541
+ this . _status = 0 ;
542
+ this . _responseText = '' ;
543
+ this . _responseType = '' as never ;
544
+ this . _responseURL = '' ;
545
+
546
+ this . _lastProgress = {
547
+ lengthComputable : false ,
548
+ loaded : 0 ,
549
+ total : 0 ,
550
+ target : this ,
551
+ } ;
552
+
553
+ if ( this . onerror ) {
554
+ this . onerror ( errorEvent ) ;
555
+ }
556
+
557
+ this . emitEvent ( 'error' , errorEvent ) ;
558
+
559
+ const loadendEvent = new ProgressEvent ( 'loadend' , this . _lastProgress ) ;
560
+
561
+ if ( this . onnloadend ) {
562
+ this . onnloadend ( loadendEvent ) ;
563
+ }
564
+
565
+ this . emitEvent ( 'loadend' , loadendEvent ) ;
566
+
567
+ this . _updateReadyStateChange ( this . DONE ) ;
568
+ } ) ;
569
+
570
+ return ;
571
+ }
572
+ }
573
+ const errorEvent = new ProgressEvent ( 'error' , this . _lastProgress ) ;
574
+ this . _status = 0 ;
575
+ this . _responseText = '' ;
576
+ this . _responseType = '' as never ;
577
+ this . _responseURL = '' ;
578
+
579
+ this . _lastProgress = {
580
+ lengthComputable : false ,
581
+ loaded : 0 ,
582
+ total : 0 ,
583
+ target : this ,
584
+ } ;
585
+
586
+ if ( this . onerror ) {
587
+ this . onerror ( errorEvent ) ;
588
+ }
589
+
590
+ this . emitEvent ( 'error' , errorEvent ) ;
591
+
592
+ const loadendEvent = new ProgressEvent ( 'loadend' , this . _lastProgress ) ;
593
+
594
+ if ( this . onnloadend ) {
595
+ this . onnloadend ( loadendEvent ) ;
596
+ }
597
+
598
+ this . emitEvent ( 'loadend' , loadendEvent ) ;
599
+
600
+ this . _updateReadyStateChange ( this . DONE ) ;
601
+
602
+ return ;
603
+ }
604
+
477
605
const url = new URL ( `file://${ path } ` ) ;
478
606
479
607
const responseURL = url . href ;
0 commit comments