Cordova 文件传输插件下载总是产生 http 状态 401 和错误代码 3

Cordova file-transfer plugin download always yields http status 401 and error code 3

我正在使用 Ionic 为 Android 构建应用程序,当从设备进行测试时,我使用 org.apache.cordova.file-transfer 插件进行的调用总是产生相同的响应。

这是用于启动下载的代码:

    $scope.getFile = function(){
      var filePath = cordova.file.dataDirectory + "beer.json";
      var uri = encodeURI('https://stormy-sierra-8448.herokuapp.com/api/?q=stones+pale+ale');
      var options = {};
      $cordovaFileTransfer.download(uri, filePath, options, true)
        .then(function(result) {
          $scope.status = result;
          alert('success');
        }, function(err) {
          console.log(err);
        }, function (progress) {
          $timeout(function () {
            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
          })
        });
    }

然后是响应(来自控制台)

FileTransferError {
  code: 3,
  source: "https://stormy-sierra-8448.herokuapp.com/api/?q=stones+pale+ale",
  target: "documents/beer.txt",
  http_status: 401,
  body: null…
}
body: nullcode: 3exception: nullhttp_status: 401source: "https://stormy-sierra-8448.herokuapp.com/api/?q=stones+pale+ale"
target: "documents/beer.txt"
__proto__: FileTransferError

我的环境是这样的: 科尔多瓦 v5.0.0 离子 1.3.20

我看到其他人 post 降级插件使其工作,但是当我低于我正在使用的当前版本 (0.5) 时,应用程序无法构建。当我使用最新版本 (1.0) 时,应用程序会构建,但在启动后,控制台显示:

Uncaught module cordova-plugin-file.ProgressEvent not found - cordova.js:59

设备已连接并已使用 'device' 插件验证。

请帮忙!

最后,我在 index.html 主文件中引导 Angular 到设备就绪事件。事件触发后,我不必担心插件没有初始化和准备好使用,我认为这是 FileTransfer 插件失败的原因之一。

我做的另一件事是为 一切 安装和使用 $iconic 命令。之前,我混合使用 $cordova [cmd]$ionic [cmd]。不确定这是否真的很重要,但现在一切似乎都在正常工作。

引导逻辑如下。我个人认为这是每个离子项目从一开始就应该设置的方式。请记住从您的 body 标签或您放置它的任何地方删除 ng-app。

angular.element(document).ready(function() { if (window.cordova) { document.addEventListener('deviceready', function() { angular.bootstrap(document.body, ['myApp']); }, false); } else { angular.bootstrap(document.body, ['myApp']); } });

仍在使用最新版本的 Cordova 和 Ionic。 :)