1

I'm currently trying to develop a generic file download button on my Apache Cordova App.

At the moment I'm using XMLHttpRequest and cordova-plugin-file as suggested in Cordova - download image from URL to the pictures gallery, however as described in that same post, it's not working for iOS.

It works perfectly for Android. However, for iOS, it seems that I can't store it anywhere and I don't get any error in the console.

I tried a few different storage locations using cordova.file.* and I already tried to remove Download from the folder link.

From what I understood the file paths in cordova.file.* are always temporary and include the app's unique ID just like this: file:///var/mobile/Containers/Data/Application/E4A79B4A-E5CB-4E0C-A7D9-0603ECD48690/tmp/cdv_photo_003.jpg

And that is not what I need, I want to download and store the file in my iOS device (maybe in the Files folder?)

I did try some different Cordova plugins but all of them seem to be pretty old and none worked. Does anyone have any solution?

  var blob = null;
  var xhr = new XMLHttpRequest();
  xhr.open("GET", fileurl);
  xhr.responseType = "blob";
  xhr.onload = function() {
    blob = xhr.response;
    console.log(blob);
    var storageLocation = "";
    switch (device.platform) {
      case "Android":
        storageLocation = 'file:///storage/emulated/0/';
        break;
      case "iOS":
        storageLocation = cordova.file.documentsDirectory;
        break;
    }
    var folderpath = storageLocation + "Download";
    var DataBlob = blob;
    window.resolveLocalFileSystemURL(folderpath, function(dir) {
      dir.getFile(filename, {
        create: true
      }, function(file) {
        file.createWriter(function(fileWriter) {
          fileWriter.write(DataBlob);
        }, function(err) {
          console.log(err);
        });
      });
    });
  }
  xhr.send();
migffp
  • 21
  • 3

1 Answers1

0

I'm having the same problem. It works fine on the xcode simulator and on an iphone connected to the mac, built as debug and release, but when I deploy the app to TestFlight and install it on the same iphone, the file downloads over https just don't work. I think it's something to do with CORS (Cross-Origin Resource Sharing) and the Cordova redirect to a local blob file but can't be sure.

This all started with Apple's UIWebView deprecation warning that vanished part way through my upgrade to WKWebView (cordova-ios@6.2.0), which has increased browser security.

  • 1
    For me it seemed that the app couldn't write files outside of it's directories. My workaround was to install FileOpener2 plugin which allows to open the file outside of the application so that the user can store the file by himself. https://github.com/pwlin/cordova-plugin-file-opener2 Hope this works for you as well. – migffp Mar 26 '21 at 18:17
  • @migffp - could you post an example of your code please? I have gotten files to donwload in iOS using both `cordova.file.externalDataDirectory` and `cordova.file.dataDirectory` (from cordova-plugin-file), my app can see the files but the user cannot acesss them. I need a method that can store the file to their iPhones `Downloads` folder, or some method that allows the user to select the path. – rolinger Mar 06 '23 at 13:48