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清單文件的文件讀寫權限。

在清單中添加權限可以解決您的問題。