我试图在
angularjs(在Ionic中)实现文件上传,但是遇到了一些问题.我在项目目录中阅读了以下命令,然后在命令下运行 –
bower install ngCordova cordova plugin add org.apache.cordova.file-transfer
然后,我在index.html中添加了所需的引用 –
<!-- ngCordova script --> <script type="text/javascript" src="lib/ngCordova/dist/ng-cordova.min.js"></script> <!-- cordova script (this will be a 404 during development) --> <script type="text/javascript" src="cordova.js"></script>
然后,我将服务注入我的控制器 –
angular.module('myApp').controller('AppController',['$scope',$cordovaFileTransfer',function ($scope,$cordovaFileTransfer)
但是,当我尝试使用它时 –
var fileTransfer = new FileTransfer(); fileTransfer.upload("server url","file path",options).then(function(result)...
我收到一个错误 –
Uncaught ReferenceError: FileTransfer is not defined AppController.js:35 angular.module.controller.$scope.uploadFile AppController.js:22 (anonymous function) n.event.dispatch jquery-2.1.3.min.js:3 n.event.add.r.handle jquery-2.1.3.min.js:3
我是AngularJS的新手,不知道这里出了什么问题.我在这里错过了参考文献吗?任何人都可以帮我解决这个问题吗?
提前致谢.
EDIT1
以下是Ionic的初始化方法 –
.run(function ($ionicPlatform) { $ionicPlatform.ready(function () { // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard // for form inputs) if (window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if (window.StatusBar) { // org.apache.cordova.statusbar required StatusBar.styleDefault(); } }); })
EDIT2
解决方法
我只是为自己说话,但可能有两个(离子:三个)原因,FileTransfer未定义.而且您不必自己定义这些对象(FileTransfer,File),只要您安装了两个插件就会定义它们:
>问题
好方法:
document.addEventListener("deviceready",onDeviceReady,false); function onDeviceReady() { // as soon as this function is called FileTransfer "should" be defined console.log(FileTransfer); }
坏方法:
// calling FileTransfer before deviceready var f = new FileTransfer(); ... document.addEventListener("deviceready",false); ... ...
>问题
还必须安装File-Plugin.在deviceready-function被称为File-Object之后“应该”定义:
document.addEventListener("deviceready",false); function onDeviceReady() { // as soon as this function is called File "should" be defined console.log(File); }
>问题(离子)
当使用离子跟随命令时,需要将插件包含到创建的平台(android,ios,blackberry)中:
ionic plugin add org.apache.cordova.file ionic plugin add org.apache.cordova.file-transfer
有时Ionic很难正确地构建您的项目,在我的情况下,由于编译问题,或者根本没有触发deviceready或者首先构建平台失败.