File-transfer download file issue on Cordova 3.1

核能气质少年 提交于 2019-12-13 14:26:35

问题


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.


回答1:


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




回答2:


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.




回答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.




回答4:


Add the Internet Permission also

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



回答5:


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.



来源:https://stackoverflow.com/questions/19978369/file-transfer-download-file-issue-on-cordova-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!