ghostCoder
ghostCoder

Reputation: 7655

viewing/downloading a file in Phonegap iOS app

Im building an iOS phonegap(v 1.3) app. In that on a page i have given a button to download a .pptx file.

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

The href of the button points to a download link on my website. So clicking on the link should automatically download the file.
Whats happening is that on clicking on the link, the file is downloaded but it opens up in the same view. All slides of the pptx come one below the other and they occupy the full screen. There is no way to go back to my app, other than killing my app and restarting. I tried target = "_blank", "_tab", nothing works, everything results in a similar behavior with the slides on the screen and no way for me to go back to my app.

Can i do something so that the .pptx file opens in another view, or better, not opens at all and just gets saved(like in android)? pls help.

Upvotes: 2

Views: 1969

Answers (2)

Adarsh V C
Adarsh V C

Reputation: 2314

For downloading and displaying a file, follow the sample code.

Include the given code just above the </head> tag in your 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>

Refer :- Blog Post

Upvotes: 1

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

I guess you could take a look at this plugin

PhoneGap plugin for downloading URL

Upvotes: 2

Related Questions