Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file via FileTransfer in Cordova

I am developing an application for Android using Cordova. I am already familiar with FileTransfer and I already know how to download a file. Problem is that when I am downloading a large file (20 MB) then this download tooks a while without notifying user that something is actually happening.

All I managed is download of file into sd card via:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);

and then show dialog to user that file was downloading and where (s)he can find it. But I want let user know about progress. Or is it possible to somehow handle this file transfer in background so user may see a downloading icon in Android's top bar just like when downloading file via default browser?

Thank you very much for any help.

Code:

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

function gotFS(fileSystem) {
    // save the file system for later access
    window.rootFS = fileSystem.root;
}

fileTransfer.download(
    http://somewhere.com/bigfile.zip,
    window.rootFS,
    function(entry) {
        console.log("download complete: " + entry.fullPath);
    },
    function(error) {
        console.log("download error source " + error.source);
    },
);
like image 861
Jan Zdrazil Avatar asked Dec 20 '22 23:12

Jan Zdrazil


1 Answers

Raymond Camden wrote a nice article about this. You can use bootstrap in order to show a progressbar when using the progress-function of PhoneGap in order to let a progress bar grow:

fileTransfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
} else {
loadingStatus.increment();
}
};

Read here

like image 131
TorchMan Avatar answered Dec 28 '22 07:12

TorchMan