Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

android - FileTransfer Cordova download path

I'm using Cordova (5.4) to create apps for Android and Iphone. All goes fine, except I want to download images using the Cordova's plugin "FileTransfer" and I having some problems with the path.

If I use the FileTransfer like this:

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

This works fine. But I would want a path that worked on Android and Iphone, (not a static one) and if it could be, that the user could see this images directly in their gallery.

Checking the plugin description I tried:

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

But this fails with the FileTrasferError:

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

Checking answers around I tried also:

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.

I'm quite lost. Anyone knows what can I do? I'm quite sure that must be a better way to do it than static routes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@Luisma,

Please find the sample code snippet to write pdf file in device using cordova file and file transfer plugin:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...