[solved] Download files on device


#1

Hello,

I’m trying to download files from a server, which has a PHP that retrieves a file when the appropiate parameters are added. First, I considered to use the FileTransfer plugin, but didn’t see the way to add these parameters to download (afterwards, I read that FileTransfer admits headers, but nevertheless I haven’t tried it yet). The thing is that now I’m using the following code:

download(user: string, token: string, oaram1: string) {
  this.userService.download(user, token, param1)
      .subscribe(
      (data) => { // Success
        this.fileURL = this.downloadFile(data, 'application/zip', 'file.zip');
        console.log("URL = " + this.fileURL);
      },
      (error) =>{
        console.error(error);
      }
    );
    }

  downloadFile(data: any, type: string, filename: string): string {
      const url = window.URL.createObjectURL(data);

      const a = document.createElement('a');
      a.setAttribute('style', 'display:none;');
      document.body.appendChild(a);

      a.href = url;
      a.download = filename;
      a.click();
      return url;
    }
  }

userService.download:
  download(user: string, token: string, param1: string) {
    const body = {user: user, token: token, param1: param1};
    return this.http.post(this.downloadUrl, body, {responseType: "blob"});
  }

This code works in the browser, downloading the file, but I’m unable to get it working on the device, iOS particularly. I’m getting this in the console with the device:

URL = blob:http://localhost:8080/digits-and-characters

Any idea about how to download the file on the device? Or maybe it is downloaded indeed, but I don’t know how to access the app’s dataDirectory. Any help is appreciated.


#2

Maybe with the native plugins File and File Opener?


#3

Hello again,

I tried listing the contents of the dataDirectory with the File plugin after downloading the file, but I’m just getting an empty list:

this.file.listDir(this.file.dataDirectory, '').then((listing) => {
        console.log('Directory listing below');
        console.log(listing);
});

Did the file actually download? Where is it? Thank you again.


#4

Hello,

I solved the issue with XMLHttpRequest and Angular’s HttpClient methods, in addition to the method described at this thread.