2021年2月4日星期四

Downloading a file from a website from a Cordova iOS app

I have a Cordova app that downloads an image file from a website and saves to the local cache. This works fine on Android, but not on iOS. I am using Cordova 10.0.0 with iOS 6.1.1

var xhr = new XMLHttpRequest();  xhr.open("GET", 'https://www.mywbsite.com/image.jpg', true);  xhr.responseType = "blob";    xhr.onreadystatechange = function () {      console.log(xhr.readyState);  }    xhr.onload = function() {            var blob = xhr.response;        window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function (directoryEntry) {          directoryEntry.getFile('downloadedFile.jpg', { create: true }, function (fileEntry) {              fileEntry.createWriter(function (fileWriter) {                  fileWriter.onwriteend = function (result) {                      console.log("Success");                  };                  fileWriter.onerror = function (e) {                      console.log("failed");                  };                  fileWriter.write(blob);              }, function(e) {                  console.log("failed");              });          }, function(e) {              console.log("failed");          });      }, function(e) {          console.log("failed");      });  }  xhr.onerror = function(e) {       console.log("failed "+xhr.status);  }    xhr.send();  

The readyState goes right to 4 and the status is 0. I suspect this has something to do with WKWebView, but I am unsure how to resolve this. The remote site is not running a script, just the direct image I want to download.

Thanks! Jon

https://stackoverflow.com/questions/66039446/downloading-a-file-from-a-website-from-a-cordova-ios-app February 04, 2021 at 12:39PM

没有评论:

发表评论