Search code examples
angularcordovaionic-frameworkfile-transfer

Can't download file now that filetransfer is deprecated


I wrote a Ionic 4 application. I wanted to update it and it seems that filetransfer is now deprecated, so I removed it. the problem is, now I can't download files. Documentation says it can be replaced by XMLHttpRequest, like in the link below, which I followed to make a new "downloadFiles" function. https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html

But this solution makes me struggle with CORS issue. I can't set the correct CORS header (allow origin and stuff) on the server because I can't edit it.

What is the right way to download files now with Ionic ?

Thanks by advance


Solution

  • Finally found a solution with the plugin cordova-plugin-advanced-http

    first install plugin

    ionic cordova plugin add cordova-plugin-advanced-http
    npm install @ionic-native/http
    

    then use it to get blob, it bypass cors issues

     import { HTTP, HTTPResponse } from '@ionic-native/http/ngx';
    
     constructor(
       private adHTTP: HTTP
     )
     ...
      const reqOptions: any = {
        method: 'get',
        responseType: 'blob'
      };
      let res: HTTPResponse = await this.adHTTP.sendRequest(source, reqOptions);
      let blob: Blob = res.data;
      await this.file.writeFile(path, filename, blob, { replace: true });