mramonlopez / cordova-plugin-file-downloader

Phonegap plugin to download a list of files or a single file to the phone, check consistency and unzip if necessary (Android and ios)
MIT License
23 stars 19 forks source link

new Cordova11 file-downloader solution #22

Open rolinger opened 1 year ago

rolinger commented 1 year ago

It looks like cordova-plugin-file-transfer is dead at the moment. That is the key plugin that is required by this plugin. I think the powers that be are working on another, revived version but I can't tell. In the mean time I did find this forked version that does work with Cordova11 (cordova-android@11): https://github.com/ns0m/cordova-plugin-ns0m-file-transfer - but it doesn't work directly with this file-downloader plugin, so I had to cannibalize the following two (working) functions from others.

And this simple function now works (using: cordova-plugin-ns0m-file-transfer):

  function DownloadFile(urlFile,newFileName,storage_location){
    var fileTransfer = new FileTransfer();
    var folderpath=storage_location+newFileName ; //The path is added here.
    var onSuccess= function(entry){
      console.log("download complete: " + entry.fullPath);
    };

    var onError=function(error) {
      console.log("download error source " + error.source);
      console.log("download error target " + error.target);
      console.log("upload error code " + error.code);
    };

    fileTransfer.download(urlFile,folderpath,onSuccess,onError);
  }   

I am going to build this out for various things, like a percent counter. And if the above fails for whatever reason, my onError is going to call another backup procedure. The primary differences between these two methods is that the FileTransfer method is slower but can handle much bigger files. The XMLHttpRequest seems a bit faster but also I have read it cannot handle large file transfers well and can timeout on bigger files.

  function dlFile(urlFile,newFileName,storage_location) {
    var req = new XMLHttpRequest();
    req.open("GET", urlFile, true);
    req.responseType = "blob";

    req.onload = function (event) {
      var blob = req.response;

      window.resolveLocalFileSystemURL(storage_location, function (directoryEntry) {
        // the 'temp.pdf' is the name you want for your file.
        directoryEntry.getFile(newFileName, { create: true }, function (fileEntry) {
          fileEntry.createWriter(function (fileWriter) {
            fileWriter.onwriteend = function (e) {
              console.log('Write of file completed.');
            };

            fileWriter.onerror = function (e) {
              console.log('Write failed');
              console.log(e) ;
            };

            fileWriter.write(blob);
          } );
        } );
      });
    };

    req.send();
  }

Both functions work.

mramonlopez commented 1 year ago

Could you create a pull request to incorporate these to the code, please?

rolinger commented 1 year ago

@mramonlopez - well, these two functions work as download alternatives to this plugin. Being that the above functions are dependent on a beta version of file-transfer I wouldn't want to incorporate changes until we know the direction of file-transfer and if it will even work with file-downloader as is. Currently it does not.