【问题标题】:viewing/downloading a file in Phonegap iOS app在 Phonegap iOS 应用程序中查看/下载文件
【发布时间】:2013-10-07 21:11:15
【问题描述】:

我正在构建一个 iOS phonegap(v 1.3) 应用程序。在那个页面上,我给出了一个下载 .pptx 文件的按钮。

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

按钮的 href 指向我网站上的下载链接。所以点击链接应该会自动下载文件。
发生的事情是,单击链接时,文件已下载,但在同一视图中打开。 pptx 的所有幻灯片都在一张下方,它们占据了整个屏幕。除了杀死我的应用程序并重新启动之外,没有办法返回我的应用程序。我尝试了 target = "_blank", "_tab",没有任何效果,一切都导致屏幕上的幻灯片出现类似的行为,我无法返回我的应用程序。

我可以做些什么让 .pptx 文件在另一个视图中打开,或者更好的是,根本不打开而只是被保存(就像在 android 中一样)?请帮忙。

【问题讨论】:

    标签: ios cordova mobile-safari download


    【解决方案1】:

    我想你可以看看这个插件

    PhoneGap plugin for downloading URL

    【讨论】:

      【解决方案2】:

      要下载和显示文件,请遵循示例代码。

      在 index.html 中的 &lt;/head&gt; 标记上方包含给定代码

      <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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        相关资源
        最近更新 更多