5

I'm making an app that needs to download images from my site and store them in the phone, but when I try phonegap shows me all the errors that could happen. What can I do to correct this =/ ?

var fileTransfer = new FileTransfer();
fileTransfer.download(
    "http://developer.android.com/assets/images/home/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);
    });

The errors shown are :

Download error source " the url used"
download error target: " the target used  "
upload error code 1

I'm using cordova 2.2.0

Here is the logcat error log:

 12-06 09:07:26.413: E/FileTransfer(2186): {"target":"\/","source":"http:\/\/developer.android.com\/assets\/images\/home\/ics-android.png","code":1}
12-06 09:07:26.413: E/FileTransfer(2186): java.io.FileNotFoundException
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer.getFileFromPath(FileTransfer.java:794)
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer.access$700(FileTransfer.java:62)
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer$4.run(FileTransfer.java:631)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.lang.Thread.run(Thread.java:856)
talles_jp
  • 143
  • 3
  • 13

2 Answers2

9

You are so close, it's just your target file that is wrong. Try:

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

You need the "file://" prefix and you can't save to "/" as you don't have permissions.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • @Simon MacDonald I was trying the same way but i am getting error saying ReferenceError: FileTransfer is not defined. what could be the issue Please help. – Deepika Lalra Jul 02 '13 at 04:30
  • Didn't worked for me. I'm testing on `genymotion android emulator` and my path is `file://storage/emulated/0/Download/nn.txt`. It can be a permission issue ? Also, how to ask for permissions to save in `/` location ? – coding_idiot May 04 '14 at 17:00
  • *update* I tried with different variations and even the same code as posted here, nothing worked for me so far. – coding_idiot May 04 '14 at 17:08
  • I was able to get it working (in cordova 3.4), using the below answer, thanks! – coding_idiot May 04 '14 at 17:25
  • 1
    For others who stumble upon, adding `file://` won't work in 3.4 – coding_idiot May 04 '14 at 18:47
2

I have tried it and this work in Android but I have not check yet in ios.And the download is successfull as I am using it in my project.

enter code here
 function fun(){
var dfd = $.Deferred(function (dfd){
var remoteFile = "Your link";
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localFileName, {create: true, exclusive: false},
    function(fileEntry) {
var localPath = fileEntry.fullPath;
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
            localPath = localPath.substring(7);
            }// You need to write IOS instead of Android

    var ft = new FileTransfer();
    ft.download(remoteFile, localPath, function(entry) {
    dfd.resolve('file downloaded');
     // Do what you want with successful file downloaded and then
    // call the method again to get the next file
    //downloadFile();
            }, fail);
             }, fail);
            }, fail);

           });
            return dfd.promise();

        }
                fun().then(function(msg){
            if(msg==="file downloaded")
            {
            alert("Download complete");
            }
            else
            {
            alert("Download error")
            }
            });
            function fail(){
            alert("error");
        }
Anuj Dubey
  • 349
  • 2
  • 11
  • I tried your code and it says `device is not defined`. Removed `device` check and now it shows the error popup. – coding_idiot May 04 '14 at 17:14
  • According to this [`bug`](https://issues.apache.org/jira/browse/CB-6094) in cordova 3.4 we have to use `.toURL()` instead of `.fullPath()`. – coding_idiot May 04 '14 at 17:24
  • 1
    If device is not defined then You have to add the permissions in app/res/xml/plugins.xml and add this line and in app/AndroidManifest.xml add this line then it will not show any error like device is not defined. – Anuj Dubey May 05 '14 at 05:23