Mohammed Azhar
Mohammed Azhar

Reputation: 259

Unable to download file in Cordova and Ionic with FileTransfer

I am looking to download a file from the internet/server on my ionic app. I've been using the File Transfer plugin to try to achieve the same but the file is not getting downloaded.

I get the success callback from the plugin but if I try to open the file from fileOpener2 plugin, I get an error that the file does not exist. I checked all of my phone's storage for the file but it is not to be found. PFB the code I used:

var fileTransfer = new FileTransfer();

var imguri = "http://cdn.wall-pix.net/albums/art-space/00030109.jpg";
var targetPath = cordova.file.dataDirectory + "testImage.jpg";
fileTransfer.download(
imguri,
targetPath,
function(entry) {
    console.log("download complete - Internal URL: " + entry.toInternalURL());
    console.log("download complete - Full Path: " + entry.fullPath);

                            $cordovaFileOpener2.open(entry.fullPath, 'image/jpeg').then(function() {
                        // file opened successfully
                        console.log("File opened successfully!");
                    }, function(err) {
                        // An error occurred. Show a message to the user
                        console.log(JSON.stringify(err));
                    });



},
function(error) {
    console.log("download error source " + error.source);
    console.log("download error target " + error.target);
    console.log("download error code" + error.code);
},
false,
{
    headers: {
        "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
    }
}
);

In $cordovaFileOpener2.open, if I give entry.toInternalURL as parameter, I get this error -> Failed to find configured root that contains /data/data/com.echidna.foodreview/files/testImage.jpg

In $cordovaFileOpener2.open, if I give entry.fullPath as parameter, I get this error ->{"status":9,"message":"File not found"}

From my file manager, I searched for the testImage.jpg file and the file was not found. That should mean the file was not downloaded in the first place, then how did the success callback get triggered?

Upvotes: 2

Views: 2533

Answers (2)

athulpraj
athulpraj

Reputation: 1577

instead of

 file.dataDirectory 

which stores the file as private and doesn't allow your file manager to read it

use

 file.externalDataDirectory 

stores the file as public and allows other apps to open it

please don't confuse with externalDataDirectory as (SD card) it uses your devices internal memory only

Upvotes: 1

Mohammed Azhar
Mohammed Azhar

Reputation: 259

The issue seems to be the new user permissions required by an app in Android 6.0 and above. The file plugins do not request the user for storage access. This has to be first requested by the app and that can be handled by another plugin. There are a few plugins for this but I am using https://github.com/NeoLSN/cordova-plugin-android-permission and this works for me!

So, before you download, check if you have write permissions on storage. If you do not, then request for the permissions(checking and requesting for permissions are handled by the plugin in the link above). After you have the access, download whatever file you want!

Cheers!

Upvotes: 1

Related Questions