Shijin TR
Shijin TR

Reputation: 7788

Cordova file download not working

I have the below function to download a file to the phone,and i called this on device ready.but it call error callback every time.

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);
    });
 }

I installed plugin as perthis link https://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html And my android manifest.xml have the following permitions.

<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" />

Upvotes: 1

Views: 2521

Answers (1)

jcesarmobile
jcesarmobile

Reputation: 53351

Try requesting the file system first

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);
          }
       );
    });
}

Upvotes: 2

Related Questions