基本Phonegap下载问题

时间:2013-07-31 14:17:42

标签: android cordova

您好,我从教程中获得了以下代码。我在设备上运行它并没有做任何事情。我是phonegap和JS的新手,所以任何人都可以帮忙。

function onBodyLoad(){

        document.addEventListener("deviceready", onDeviceReady, false);
}


function downloadFile(){

         window.requestFileSystem(
                         LocalFileSystem.PERSISTENT, 0, 
                         function onFileSystemSuccess(fileSystem) {
                         fileSystem.root.getFile(
                                     "dummy.html", {create: true, exclusive: false}, 
                                     function gotFileEntry(fileEntry){
                         var sPath = fileEntry.fullPath.replace("sample.html","");

                                     var fileTransfer = new FileTransfer();
                                     fileEntry.remove();

                         fileTransfer.download(
                             "http://www.webfoxers.com/DevScope.pdf",function(theFile) {

                             console.l og("download complete: " + theFile.toURI());
                                     showLink(theFile.toURI());
                                               },
                                    function(error) {
                                     console.log("download error source " + error.source);
                                               console.log("download error target " + error.target);
                                               console.log("upload error code: " + error.code);
                                               }
                                               );
                                     }, 
                                     fail);
                         }, 
                         fail);

        }

        function showLink(url){
            alert(url);
            var divEl = document.getElementById("ready");
            var aElem = document.createElement("a");
            aElem.setAttribute("target", "_blank");
            aElem.setAttribute("href", url);
            aElem.appendChild(document.createTextNode("Ready! Click To Open."))
            divEl.appendChild(aElem);

        }


        function fail(evt) {
            console.log(evt.target.error.code);
        }


    function onDeviceReady()
    {
    // do your thing!
            downloadFile();
    }

这是来自eclipse的Logcat文件:07-31 13:25:52.632:

D/DroidGap(4366): DroidGap.onCreate()
07-31 13:25:52.712: D/dalvikvm(4366): GC_EXTERNAL_ALLOC freed 129K, 48% free 2872K/5447K, external 2032K/2137K, paused 68ms
07-31 13:25:52.772: D/CordovaWebView(4366): Origin to allow: http://127.0.0.1*
07-31 13:25:52.772: I/CordovaLog(4366): Found log level DEBUG
07-31 13:25:52.772: I/CordovaLog(4366): Changing log level to DEBUG(3)
07-31 13:25:52.772: I/CordovaLog(4366): Found preference for useBrowserHistory=false
07-31 13:25:52.792: D/CordovaLog(4366): Found preference for useBrowserHistory=false
07-31 13:25:52.812: D/DroidGap(4366): DroidGap.init()
07-31 13:25:52.822: D/CordovaWebView(4366): >>> loadUrl(file:///android_asset/www/index.html)
07-31 13:25:52.822: D/PluginManager(4366): init()
07-31 13:25:52.862: D/CordovaWebView(4366): >>> loadUrlNow()
07-31 13:25:52.882: D/DroidGap(4366): onMessage(onPageStarted,file:///android_asset/www/index.html)
07-31 13:25:52.882: D/SoftKeyboardDetect(4366): Ignore this event
07-31 13:25:52.992: D/SoftKeyboardDetect(4366): Ignore this event
07-31 13:25:53.092: D/Cordova(4366): onPageFinished(file:///android_asset/www/index.html)

07-31 13:25:53.092: D/CordovaWebView(4366): >>> loadUrlNow()

07-31 13:25:53.112: D/DroidGap(4366): onMessage(onNativeReady,null)

07-31 13:25:53.122: D/DroidGap(4366): 

onMessage(onPageFinished,file:///android_asset/www/index.html)
07-31 13:25:55.142: D/DroidGap(4366): onMessage(spinner,stop)

1 个答案:

答案 0 :(得分:0)

从您发布的代码中,file.download(来自内存)的第二个参数是文件路径的保存位置。我想你在代码中错过了这个。

此致 拉杜