brent sun
brent sun

Reputation: 51

Uncaught ReferenceError: FileTransfer is not defined (using cordova 2.7.0)

I want to use FileTransfer to download a file from web server, code is as following:

  function downloadFile(url) {
     var fileTransfer = new FileTransfer();
     var uri = encodeURI(url);
     var filepath="www/download/";

     fileTransfer.onprogress = function(progressEvent) {
        if (progressEvent.lengthComputable) {
          loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
        } else {
          loadingStatus.increment();
        }
     };

    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: {

        }
    }
  );
 }

when I run my app in simulator or real devide, all hit error message: Uncaught ReferenceError: FileTransfer is not defined.

I have included cordova.js, what is the reason for this error? thanks.

rgds brent

Upvotes: 3

Views: 4615

Answers (2)

Agu Dondo
Agu Dondo

Reputation: 13569

This plugin doesn't work from the browser, it will throw a “new FileTransfer is not defined” error. Use a real device to test.

Upvotes: 2

cha
cha

Reputation: 11

You need to install phonegap plugin:

$phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
$phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git

If you already install, probably you need rebuild phonegap platforms:

$phonegap build android

Upvotes: 1

Related Questions