Cordova File Transfer - Deprecated

#1

Hello,

With the Cordova File Transfer being deprecated, has anyone created an alternate means of downloading large binary files across Devices/OS? According to the cordova-plugin-file-transfer git, the preferred method is to use XMLHttpRequest.

I have not found any good examples for downloading a binary file other then using the cordova plugin.

Can the Ionic http-client be used to download large binary files? If so, any examples out there?

Ron

#2

Yes, the Angular HttpClient allows you to specify a response type, and “blob” is one option, which will give you one of these. At that point, I suppose it depends on what you want to do with it. You can also use the progress events described in here to do things like progress bars for large transfers.

#3

Thank you for the link to the Blob constructor. According to the compatibility it doesn’t look like it is well supported on mobile yet.

I am looking to be able to download files to local storage for the app to utilize while offline. Looking to support zips, video formats, images and documents. So after download the app will need access to it.

The file-transfer plugin made this so much easier. Does anyone have a working example using the http-client for downloading files for the app to access?

#4

Hi,
I’ve the same problem, did you find an example of how to use http client to download remote files from mobile?

Thank you

cld

#5

We used the method detailed here to download the file an then passed it to the File plugin using the a FileWriter.

Pseudo code:

fs.root.getFile('video.mp4', { create: true, exclusive: false }, function (file) {
      ft = new XMLHttpRequest();
      ft.open("GET", url, true);
      ft.responseType = 'arraybuffer';
      ft.addEventListener('load', () => {
        if (ft.status === 200) {
          let blob = new Blob([this.ft.response], { type: 'arraybuffer' });
          file.createWriter((fileWriter: FileWriter) => {
            fileWriter.write(blob);
          });
      });
      ft.send();
});

This method works as long as your files are small. Once you get above 50Mb the WKWebView basically crashes as it tries to convert the arraybuffer into base64 to pass to the File plugin. In the end we’ve decided just to keep using the File Transfer plugin as the download is completely native and it doesn’t stress the webview.

1 Like
#6

@smizoguchi,

Thank you for the replay and sample. Like you point out, it works for small files but large ones cause issues. I am sure this is why, though the Cordova File Transfer is deprecated, everyone is still using it. Hopefully it will be maintained until there is a true alternative.

Ron