4

我想在angularjs(in Ionic)中实现文件上传,但得到一些问题。我读,我跑以下命令的文档以下,而在项目目录是 -FileTransfer未定义 - AngularJS

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

这里是离子如何初始化 -

.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

AppController.js代码here

+0

什么时候初始化FileTransfer对象?我读了一些问题,即使deviceready被解雇了,一些插件可能还没有出现。否则尝试添加文件插件以及它在这里陈述:http://stackoverflow.com/questions/19544352/phonegap-new-filetransfer-is-not-defined – Blauharley 2015-03-08 18:40:17

+0

@Blauharley我不太确定你的意思是初始化FileTransfer对象。你能指出我应该在哪里检查? – Sam 2015-03-08 18:51:49

+0

忘记这一点,我不认为这是你的错误信息的问题。你在安装文件插件后再试一次吗? – Blauharley 2015-03-08 18:54:04

回答

2

我只是说我自己,但可以有两个(离子:三个)FileTrans的原因fer是不确定的。你不必定义这些对象(文件传输,文件)你自己,他们是因为你安装了-插件一旦定义:

  1. 问题

    好办法:

    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", onDeviceReady, false); 
    ... 
    ... 
    
  2. 问题

    也必须安装File-Plugin。 deviceready-功能被称为文件的对象后“应当”来定义:

    document.addEventListener("deviceready", onDeviceReady, false); 
    
    function onDeviceReady() { 
        // as soon as this function is called File "should" be defined 
        console.log(File); 
    } 
    
  3. 版本(离子)

    当使用离子型以下命令需要包括插件成创建平台(机器人,IOS黑莓):

    ionic plugin add org.apache.cordova.file 
    ionic plugin add org.apache.cordova.file-transfer 
    

有时离子已经得到了困难,正确地构建你的项目,在我的情况下,或者deviceready根本没有被解雇或建立一个平台失败由于编译问题,首先编辑。

+0

当我使用Ionic时,请注意编辑1 – Sam 2015-03-08 19:52:48

+0

在它的引擎盖下,它应该做什么但仍然可以附加deviceready-listener – Blauharley 2015-03-08 19:55:30

+0

我也使用离子,并使用命令“ionic plugin add org”来安装插件。 cordova ...“将其包含到平台文件夹(android,ios)中。 – Blauharley 2015-03-08 19:58:16

1

在我的IONIC项目中使用这个插件时,我也陷入了这个问题。

建议:检查Android清单文件的文件读写权限。

在清单中添加权限可以解决您的问题。