Yako
Yako

Reputation: 3484

Downloading a file with Cordova

Despite the many examples found on the documentation or in this forum, I can't find a way to download a file using Cordova...

First, I get the rootFS:

function gotFS(fileSystem) {
    console.log("got filesystem");
    // save the file system for later access
    console.log(fileSystem.root.fullPath);
        // displays "/" on desktop
        // displays "file:///mnt/sdcard" on android with SD Card
    window.rootFS = fileSystem.root;
}

document.addEventListener('deviceready', function() {                
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function(){
        console.log("error getting LocalFileSystem");
    });
}, false);

The upload script:

// Creating the image directory
imageDir = rootFS.getDirectory("imagesContent", {create: true},
    function(){
        // Success
    },
    function(error){
        console.log("ERROR getDirectory");
        console.log(error);
    }
);

// Creating and Downloading the image
    imgFile = imageDir.getFile(filename, {create: true, exclusive: true},
        function (){     
            var localPath = rootFS.fullPath+'/imagesContent/'+filename;
            fileTransfer = new FileTransfer();        
            fileTransfer.download('http://example.com/images/'+filename,
                localPath,
                function(entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);
                    console.log('download error: ' + error.code);
                    console.log("download error source " + error.source);
                    console.log("download error target " + error.target);
                }
            );
        },
        function (error){
            console.log("ERROR getFile");
            console.log(error);
        }
    );

I get this error in the console:Uncaught TypeError:

Cannot call method 'getFile' of undefined

The uri is authorized in config.xml.

Upvotes: 3

Views: 10876

Answers (2)

omar
omar

Reputation: 1

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
    uri,
    filePath,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

I use it but success and download is completed but not found image in y app

Upvotes: 0

MBillau
MBillau

Reputation: 5376

I think that the problem is with this line: imageDir = rootFS.getDirectory("imagesContent", {create: true},function(), function()) I don't think the call to getDirectory is supposed to actually return the directory name, like what you expect, which is why your imageDir object is undefined.

It might be easier for you to use the FileTransfer.download() method to download stuff from your server: http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileTransfer

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");

fileTransfer.download(
    uri,
    filePath,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
    },
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }
);

Upvotes: 2

Related Questions