我正在使用 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
我的环境是这样的: Cordova v5.0.0 ionic 1.3.20
我看到其他人发帖称降级插件后它可以正常工作,但是当我低于我正在使用的当前版本 (0.5) 时,应用程序无法构建。当我使用最新版本 (1.0) 时,应用程序会构建,但在启动后,控制台显示:
未捕获模块 cordova-plugin-file.ProgressEvent 未找到 - cordova.js:59
设备已连接并已通过“设备”插件验证。
请帮忙!
最佳答案
最后,我将 Angular 引导到我的主 index.html 文件中的设备就绪事件。事件触发后,我不必担心插件没有初始化和准备好使用,我认为这是 FileTransfer 插件失败的原因之一。
我做的另一件事是为所有 安装和使用$iconic
命令。之前,我混合使用 $cordova [cmd]
和 $ionic [cmd]
。不确定这是否真的很重要,但现在一切似乎都在正常工作。
引导逻辑如下。我个人认为这是每个 ionic 项目从一开始就应该设置的方式。请记住从您的正文标签或您放置它的任何地方删除 ng-app。
angular.element(文档).ready(函数() {
如果(window.cordova){
document.addEventListener('deviceready', function() {
angular.bootstrap(document.body, ['myApp']);
}, 错误的);
} 别的 {
angular.bootstrap(document.body, ['myApp']);
}
});
仍在使用最新版本的 Cordova 和 Ionic。 :)
关于javascript - Cordova 文件传输插件下载总是产生 http 状态 401 和错误代码 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930066/