FileTransfer Cordova Download Path

I use Cordova (5.4) to create applications for Android and Iphone. Everything is going well, except that I want to upload images using the Cordova plugin " FileTransfer ", and I have some problems with this path.

If I use FileTransfer as follows:

       uri = encodeURI('http://example.com/myImage.png'),
            fileURL = '/sdcard/Download/' + 'myImage.png',
fileTransfer.download(
                uri,
                fileURL,
                function (entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);
                },
                false,
                {
                    headers: {
                        "authorization": 'Bearer ' + token
                    }
                }
            );

It works great. But I needed a way that worked on Android and Iphone (and not on static), and if it could be so that the user could see this image directly in his gallery.

Checking the plugin description I tried:

fileURL = 'cdvfile://localhost/persistent/myImg.png'

But this does not work with FileTrasferError:

"/data/data/com.aco.plus/files/files/myImg.png: open failed: ENOTDIR (not a directory)"

Checking the answers, I also tried:

uri = encodeURI('http://example.com/myImage.png');

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {

            fileTransfer.download(
                uri,
                fileSystem.root.toURL() + '/' + 'myImg.png',
                function (entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);

                },
                false,
                {
                    headers: {
                        "authorization": 'Bearer ' + token
                    }
                }
            );
        });

And I got the same error.

. - , ? , , .

+2
2

@Luisma,

, , pdf , :

var fileTransfer = new FileTransfer();

if (sessionStorage.platform.toLowerCase() == "android") {
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
} else {
    // for iOS
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
}

function onError(e) {
    navigator.notification.alert("Error : Downloading Failed");
};

function onFileSystemSuccess(fileSystem) {
    var entry = "";
    if (sessionStorage.platform.toLowerCase() == "android") {
        entry = fileSystem;
    } else {
        entry = fileSystem.root;
    }
    entry.getDirectory("Cordova", {
        create: true,
        exclusive: false
    }, onGetDirectorySuccess, onGetDirectoryFail);
};

function onGetDirectorySuccess(dir) {
    cdr = dir;
    dir.getFile(filename, {
        create: true,
        exclusive: false
    }, gotFileEntry, errorHandler);
};

function gotFileEntry(fileEntry) {
    // URL in which the pdf is available
    var documentUrl = "http://localhost:8080/testapp/test.pdf";
    var uri = encodeURI(documentUrl);
    fileTransfer.download(uri, cdr.nativeURL + "test.pdf",
        function(entry) {
            // Logic to open file using file opener plugin
        },
        function(error) {
            navigator.notification.alert(ajaxErrorMsg);
        },
        false
    );
};
+2

Source: https://habr.com/ru/post/1665837/


All Articles