2012-02-23 39 views
2

Im構建iOS phonegap(v 1.3)應用程序。在這個頁面上,我給了一個按鈕來下載一個.pptx文件。在Phonegap iOS應用程序中查看/下載文件

<a href="'+downloadUrl+'" target="_blank">Download file</a> 

按鈕的href指向我網站上的下載鏈接。所以點擊鏈接應該會自動下載文件。
發生什麼事是,點擊鏈接時,文件被下載,但它在相同的視圖中打開。 pptx的所有幻燈片都會一個在另一個下方,並佔據整個屏幕。沒有辦法回到我的應用程序,除了殺死我的應用程序並重新啓動。我嘗試了target =「_blank」,「_tab」,沒有任何工作,所有的結果都與屏幕上的幻燈片有類似的行爲,並且沒有辦法讓我回到我的應用程序。

我可以做些什麼,讓.pptx文件在另一個視圖中打開,或者更好,根本不打開,只是得到保存(像在android中)?請幫助。

回答

1

,下載和顯示文件,請按照示例代碼。

包含給定的代碼只是</head>標籤上面您的index.html

<script type="text/javascript" charset="utf-8"> 
     // Wait for Cordova to load 
    document.addEventListener("deviceready", onDeviceReady, false); 
    // Cordova is ready 
    function onDeviceReady() { 
     alert("Going to start download"); 
     downloadFile(); 
    } 

    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("dummy.html",""); 
                 var fileTransfer = new FileTransfer(); 
                 fileEntry.remove(); 
                 fileTransfer.download(
                       "http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf", 
                       sPath + "theFile.pdf", 
                       function(theFile) { 
                       console.log("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("deviceready"); 
     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); 
    } 

    </script> 

參見: - Blog Post

相關問題