Download File to Device by xmlhttprequest

Hello
i want use xmlhhtprequest for download file from url to android/ios device
best discussion that i found is here

i use 2 sample code that users offered it but they not working (there isn’t any file in files directory of app)
i test them in simulator (android 10) and real device (android 8)

code 1 :

downloadImage(pictureUrl) {
    //new request
    const req = new HttpRequest('GET', pictureUrl, {
      reportProgress: true,
      responseType: "blob"//blob type pls
    });
    //all possible events
    this.http.request(req).subscribe((event: HttpEvent<any>) => {
      switch (event.type) {
        case HttpEventType.Sent:
          console.log('Request sent!');
          break;
        case HttpEventType.ResponseHeader:
          console.log('Response header received!');
          break;
        case HttpEventType.DownloadProgress:
          //i use a gloal progress variable to save progress in percent
          this.progress = Math.trunc(event.loaded / event.total * 100);
          break;
        case HttpEventType.Response:
          //do whatever you have to do with the file using event.body
          console.log('😺 Done!', event.body);
          //i'm gonna write the file in my case
          let result = this.file.writeFile(this.file.dataDirectory, "mymp3.jpg", event.body, { replace: true }).then((file) => {
            console.log(result);

          }).catch(
            (err) => {
              //in case of file.writefile fail
            });
      }
    });
  }

log in simulator :
1

code 2 :

newDownload(url) {
    this.http.get(url, { responseType: 'blob' })
      .subscribe((imageBlob: Blob) => {
        // imageBlob is the binary data of the the image
        // From here you can manipulate it and store it where you want
        // For example, to store it in your app dir
        // The replace true is optional but is just in case you want to overwrite it
        let result = this.file.writeFile(this.file.dataDirectory, "my_downloaded_image.jpg", imageBlob, { replace: true });
        console.log(result);

      });
  }

log in simulator :
2


Ionic CLI : 6.1.0
Ionic Framework : @ionic/angular 5.0.1
@angular-devkit/build-angular : 0.803.24
@angular-devkit/schematics : 8.1.3
@angular/cli : 8.1.3

any body test this method on ionic 5 ?
Thanks :pray:

I’m not clear on a few things:

  • why are you convinced the problem lies with the download and not the writing of the file?
  • why are you logging result and ignoring file?
  • what are you using to write to the filesystem?
  • if the answer isn’t Capacitor’s Filesystem, can we try using that?