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();