0

嗨,我有以下从教程中获得的代码。我已经在设备上运行它,它什么也没做。我是 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:07-31 13:25:52.632 的 Logcat 文件:

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)
4

1 回答 1

0

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

问候,拉杜

于 2013-09-25T02:48:54.350 回答