Cordova文件下载不起作用

时间:2015-07-24 04:46:48

标签: android file cordova

我有以下功能将文件下载到手机,我在设备就绪时调用了它。但每次都调用错误回调。

function downloadFile(){
 var fileTransfer = new FileTransfer();
 fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    "file://sdcard/ics-android.png",
    function(entry) {
        alert("download complete: " + entry.fullPath);
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("upload error code" + error.code);
    });
 }

我安装了插件作为tshis链接https://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html 我的android manifest.xml有以下内容。

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

1 个答案:

答案 0 :(得分:1)

首先尝试请求文件系统

function downloadFile(){
    var downloadUrl = "http://developer.android.com/assets/images/home/ics-android.png";
    var fileName = "ics-android.png";

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
       var fileTransfer = new FileTransfer();
       fileTransfer.download(
          downloadUrl,
          fileSystem.root.toURL() + '/' + fileName,

          function (entry) {
             alert("download complete: " + entry.fullPath);
          },
          function (error) {
              alert("download error source " + error.source);
              alert("download error target " + error.target);
              alert("upload error code" + error.code);
          }
       );
    });
}