简体   繁体   中英

Phonegap fileTransfer.Download is not working on ios phonegap 3.0

I am developing an app with JQM and then using build.phonegap.com, i am building the app.

I have a feature to download the audio file from the server and then play it in the app.

it is working perfectly in android.

I tried all possibilities to make it work.

can some one help on this.

here is my complete file handling code

fileSystem.root.getDirectory("auidofiles", {
        create : true,
        exclusive : false
    }, function(dirEntry) {     


        dirEntry.getFile(mp3_file, {
            create : false,
            exclusive : false
        }, function(fileEntry) {    

            alert("File Already downloaded");               

            if (device.platform == "Android") { 
                FILEPATH = fileEntry.fullPath;
                //FILEPATH = fileEntry.toURI();
                playAudio();                    
            } else {
                var filePathLocal1 = getPhoneGapPath() + "auidofiles/" +  mp3_file; 
                //var filePathLocal = fileSystem.root.toURL() + "auidofiles/" +  mp3_file;  
                //alert("Ios File Path:" + filePathLocal);
                //FILEPATH = filePathLocal;
                //FILEPATH = fileEntry.fullPath;
                //FILEPATH = fileEntry.toURI();
                playAudio();
            }


        }, function(fileFail) {

            alert("File Not found");

            var ft = new FileTransfer();

            ft.onprogress = function(progressEvent) {

                if (progressEvent.lengthComputable) {
                    var perc = Math.floor(progressEvent.loaded
                            / progressEvent.total * 100);
                    $('#downloadStatus').html(perc + "% loaded...");
                } else {

                    if ($('#downloadStatus').html() == "") {
                        $('#downloadStatus').html("Loading");
                    } else {
                        $('#downloadStatus').html(
                                $('#downloadStatus').html() + ".");
                    }
                }
            }; //ft.Progress

            var filePathLocal = fileSystem.root.toURL() + "auidofiles/" +  mp3_file;    
            ft.download(audioStreamUrl, filePathLocal, function(entry) {    

                //alert("File = " + entry.toURI()); 
                if (device.platform == "Android") {
                    FILEPATH = entry.fullPath;
                    //FILEPATH = entry.toURI();
                    playAudio();
                } else {
                    var filePathLocal1 = getPhoneGapPath() + "auidofiles/" +  mp3_file; 
                    //alert("Ios File Path:" + filePathLocal);
                    FILEPATH = filePathLocal;
                    //FILEPATH = entry.fullPath;
                    //FILEPATH = entry.toURI();
                    playAudio();
                }

            }, function(error) {

                $('#downloadStatus').html(
                        "download error source " + error.source);
                $('#downloadStatus').html(
                        "download error target " + error.target);
                $('#downloadStatus').html("upload error code" + error.code);

            }); //ft.Download           
        }); //getFile End

    }, function(dirfail) {
        alert("dirfail");   
        alert(JSON.stringify(dirfail))
        $('#downloadStatus').html(JSON.stringify(dirfail));
    })

function getPhoneGapPath() {

var path = window.location.pathname;
path = path.substr( 0, path.length - 10 );
return 'file://' + path;

};

There is a new requirement to use .toURL() instead of .fullPath

This fixes the majority of these type of problems - however I have experienced a similar problem using older versions of the iOS (5.x) where filetransfer.download never fires success/fail/progress events even though it does actually download the file. I then get an app crash when I try to restart the app...

我遇到了同样的问题, 并通过使用Phonegap / Cordova v3.3将Cordova Plugin FileTransfer从v0.4.2降级到v0.3.3进行了修复

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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