3

I'm creating my first webapplication using cordova 3.1.

In this app I need to be able to download a file to the phone and then open it, but i cant seem to get pass how to download the file.

I'm using the file-transfer code from cordovas doc page. Everyting is installed with CLI.

This is how far I've come:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

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

function onDeviceReady() {
   download();
}    

function download(){
        var filePath = '/mnt/sdcard';
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("https://www.dropbox.com/s/27bxw65u4ga5is0/test.pdf");

        fileTransfer.download(
            uri,
            filePath,
            function(entry) {
                console.log("download complete: " + entry.fullPath);
            },
            function(error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code" + error.code);
            },
            false,
            {
                headers: {
                    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                }
            }
        );
}

config.xml

<access origin="*" />
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>

and my manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The errors Im getting

 E/FileTransfer(878):     {"target":"\/sdcard\/test.pdf","source":"https:\/\/www.dropbox.com\/s\/27bxw65u4ga5is0\/tes t.pdf","http_status":0,"code":1}
 E/FileTransfer(878): java.io.FileNotFoundException:/sdcard/test.pdf: open failed: EACCES (Permission denied)
 E/FileTransfer(878):   at libcore.io.IoBridge.open(IoBridge.java:416)         E/FileTransfer(878):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
 E/FileTransfer(878):   at    org.apache.cordova.CordovaResourceApi.openOutputStream(CordovaResourceApi.java:290)
 E/FileTransfer(878):   at org.apache.cordova.CordovaResourceApi.openOutputStream(CordovaResourceApi.java:271)
 E/FileTransfer(878):   at org.apache.cordova.filetransfer.FileTransfer$4.run(FileTransfer.java:711)

I've spent almost 2 days now trying to solve this issue without success.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
trion
  • 31
  • 1
  • 3

5 Answers5

7

Not sure about 3.1, but for the latest version of Cordova (3.3+), the newer (1.0.0+) version of File uses filesystem URLs instead of the file path. Something like this:

cdvfile://localhost/persistent/path/to/file

See the notes at the bottom of this doc: https://github.com/apache/cordova-plugin-file-transfer/blob/31ac00d3ae35f9ca280cf4e6c9edc9df23ea95b5/doc/index.md

dsims
  • 1,322
  • 1
  • 15
  • 24
4

cordova 3.3.0 cordova-plugin-file-transfer r 0.4.2 cordova-plugin-file r 1.0.1

TESTED on real devices, works both for Android and iOS

my code:

fileTransfer.download(
    "http://my.domain.com/data/sashaTest.txt",
    fileSystem.root.toURL() + "sashaTest.txt", // the key factor !!!
    function(theFile) {
    alert("download complete: " + theFile.toURL());
    console.log("download complete: " + theFile.toURL());
    },
    function(error) {
    console.log("download error source " + error.source);
    console.log("download error target " + error.target);
    console.log("upload error code: " + error.code);
    },
    true
);

the target set to: fileSystem.root.toURL() + "sashaTest.txt",

made the difference.

sasha
  • 365
  • 1
  • 15
3

yup, fileSystem.root.toURL() is the new player out there and lets you use the latest cdvfile:// url system of Cordova.

Too bad they didn't shared a general notice on this right away. It showed up in a resolved issue's comments and later was documented on the documentation in their GIT repo as dsims mentioned earlier.

Once the toURL() thing is clear the download samples as Phonegap/Cordova docs state make sense and will work out.

Community
  • 1
  • 1
EeKay
  • 6,494
  • 3
  • 24
  • 24
0

Add the Internet Permission also

<uses-permission android:name="android.permission.INTERNET" />
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • Thanks for the answer but the internet permission snippet is already in the manifest, just forgot to mention it. – trion Nov 14 '13 at 13:20
0

Note: This was originally edited into the question by the OP. I've moved it to an answer, to be in line with site guidelines.


Removed Cordova 3.1 and installed 2.7.0 manually and got it to work.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189