简体   繁体   English

如何使用PhoneGap在Android SdCard中下载和保存文件(APK)?

[英]How to download and save a file(APK) in android SdCard using PhoneGap?

I am trying to download and save apk from server url. 我正在尝试从服务器URL下载并保存apk。 But im unable to download and save that. 但即时通讯无法下载和保存。 Can you please help me on this? 你能帮我吗?

Code: 码:

onDeviceReady: function() {

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
app.receivedEvent('deviceready');
myApp.start(); //this is where I put the call to my App's functionality relying on device APIs
}


    function gotFS(fileSystem) {
            window.fileSystem = fileSystem;
            fileSystem.root.getDirectory(window.appRootDirName, {
                create: true,
                exclusive: false
            }, dirReady, fail);
        }

        function dirReady(entry) {
            window.appRootDir = entry;
            console.log("application dir is ready");
           // alert("ready");
             var fileTransfer = new FileTransfer();

            var url = "http://www.something.org/Android/Sample.apk";

            var filePath = window.appRootDir.toURL() + "Sample.apk";
                            alert(filePath);

             var uri = encodeURI(url.android);
            fileTransfer.download(
            uri, filePath, function(entry) {
                alert("download complete: " + entry.fullPath);

        window.plugins.webintent.startActivity({
        action: window.plugins.webintent.ACTION_VIEW,
        url: 'file://' + entry.fullPath,
        type: 'application/vnd.android.package-archive'
        },
        function(){
            console.log("Success");
        },
        function(e){
            alert('Error launching app update');
        }
    );          


            }, function(error) {
                alert("download error" + error.codename);
            });
        }
  var url = "http://www.something.org/Android/Sample.apk";
                        var targetPath = "file:///storage/sdcard0/Download/aplstar" + APPVER + ".apk"; 
                        var trustHosts = true
                        var options = {};
                        $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) {

                            $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive'
                            ).then(function () {
                                $ionicLoading.show({
                                    template: "Please select appropriate option to update"
                                });
                                                                }, function (err) {
                                //
                                $ionicLoading.show({
                                    template: "Unable to update."
                                });
                            });
                            $ionicLoading.hide();
                        }, function (err) {
                            alert('failed download progress');
                        }, function (progress) {
                            $timeout(function () {
                                var downloadProgress = (progress.loaded / progress.total) * 100;
                                if (downloadProgress) {
                                    var progressBar = document.getElementById('progressbar').children[0];
                                    var count = document.getElementById('count');
                                    count.innerHtml = parseInt(downloadProgress).toString() + '%';
                                    progressBar.style.width = downloadProgress + '%';
                                }

                                if (downloadProgress > 99) {
                                    $ionicLoading.hide();
                                }
                            })
                        });
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM