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

情到浓时终转凉″ 提交于 2020-03-01 10:05:50

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/16442698/uncaught-referenceerror-filetransfer-is-not-defined-using-cordova-2-7-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!