简体   繁体   中英

How to use FileTransfer.download on Android

I use Phonegap 1.9.0 on Android, but there are few samples on the web, so I worry about download of a file.

var ft = new FileTransfer();
ft.download(
    "http://www.something.com/test.zip",
    "test.zip", // <- Trouble 1
    function(entry) {
        alert("success");
    },
    function(err) {
        alert(err); // <- Trouble 2
    }
);

1.I don't understand the way of specifying a file path suitable for this argument. How should I specify a local path? Or is there any required Android.permission?
2.The message "Class not found" is shown. What is this cause?
3.What is the path in native Java of the downloaded file?

Yeah the Cordova/Phonegap docs are very lite on real world examples!

  1. Simon Mac Donald has a great post on downloading: http://simonmacdonald.blogspot.co.uk/2012/04/sorry-for-being-gone-so-long-vacation.html If you want to download multiple files, check out his gist: https://gist.github.com/3835045

  2. I think FileTransfer is only available on the device (maybe the emulator?), but I also get this error in the browser - maybe someone else can chip in with an explanation/workaround for this one.

  3. This will depend on the platform, but it can be found with FileSystem.root.fullPath. If you are appending filenames, you'll need to add a slash.

// This worked for me
var ft = new FileTransfer();
ft.download(
  "http://www.something.com/test.zip", // what u download
  "/sdcard/test.zip", // this is the filename as well complete url
  // fileSystem.root.toURL() + "test.zip",  use ios and others
  function(entry) {
    alert("success");
    alert(JSON.stringify(entry));

  },
  function(err) {
    alert(err);
    alert(JSON.stringify(err));
  }
);

You can check directory structure in ADT Eclipse DDMS Perspective -> File Explorer

var ft = new FileTransfer();
ft.download(
    "http://www.something.com/test.zip", // what u download
    "", // this is dir which u download, right now, it will download to mnt/sdcard/
    function(entry) {
        alert("success");
    },
    function(err) {
        alert(err); 
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM