Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Download a file and stored locally with Cordova Android/iOS apps

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.46k
    Comment on it

    Hello Readers,

    In this post we will learn how to download a file and stored it locally with Cordova Android/iOS apps.

     

    First, we need to install cordova file transfer plugin via CLI :

    $ cordova plugin add cordova-plugin-file-transfer

     

    Example :

    $scope.download = function()
    {
        var pdfUrlBase = 'http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf';
        var pdfName = pdfUrlBase.split("http://www.w3.org/2011/web-apps-ws/papers/");
        var fileSystem; 
        var statusDom;
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
            fileSystem = fs;
            statusDom = $("#statusDom");
            var ft = new FileTransfer();
            var uri = encodeURI(pdfUrlBase);
            if(device.platform == 'Android')
            {
                var downloadPath = cordova.file.externalDataDirectory+ "/demoApp/"
                                   + pdfName[1];
            }
            else
            {
                var downloadPath = cordova.file.dataDirectory+ "/demoApp/" 
                                   + pdfName[1]; 
            }
            $("#statusDom").html("<p>loading...</p>");
            ft.onprogress = function(progressEvent) {
                if (progressEvent.lengthComputable) 
                {
                    var perc = Math.floor(progressEvent.loaded / progressEvent.total
                    * 50);
                    $("#statusDom").html(  "<p> downloading...</p>");
                } 
                else 
                {
                    $("#statusDom").html( "<p> 100 % loaded...</p>");
                }
            };
            ft.download(uri, downloadPath, function(entry) 
            {
                $("#statusDom").html("<p> Download complete</p>");
            }, 
            function(error) {
                console.log('Crap something went wrong...'+JSON.stringify(error));  
            });
        }, 
        function(e) {
        });
    }

     

    In the above example we are using download method and onprogress property of file transfer plugin.

    • download: Downloads files from server. In download method i am using two parameters uri and downloadPath. uri is URL of server to download the file which is encoded by encodeURI(). downloadPath is path where we want to store downloaded file. It can be the full path of the file on the device.
    • onprogress: This property called with a progressEvent in a function. It works when new data is uploading/downloading.

    So with the help of Cordova file transfer plugin we can download a file from server.

    Hope this will help :)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: