2016-03-21 12 views
0

ユーザーがファイルをアップロードできるようにする簡単なアプリケーションを構築しようとしていて、「追加」ボタンをクリックすると、ファイルが解析され、結果がブラウザに表示されます。ファイルをアップロードしてAngularJSで解析する際の問題

私はIntelliJを使ってAngularJSアプリケーションスタブを生成し、それに応じて修正しています。

私の試みは、以下の通りです:

view1.html

<!DOCTYPE html> 
<html lang="en" ng-app> 
<head> 
    <meta charset="utf-8"> 
    <title>My HTML File</title> 
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css"> 
    <link rel="stylesheet" href="../app.css"> 
    <script src="bower_components/angular/angular.js"></script> 
    <script src="bower_components/ng-file-upload/ng-file-upload-shim.js"></script> <!-- for no html5 browsers support --> 
    <script src="bower_components/ng-file-upload/ng-file-upload.js"></script> 
    <!--<script src="view1.js"></script>--> 
</head> 

<body> 
    <div ng-controller="View1Ctrl"> 
     <input type="file" id="file" name="file"/> 
     <br/> 
     <button ng-click="add()">Add</button> 
     <p>{{data}}</p> 
    </div> 
</body> 

</html> 

view1.js

'use strict'; 

angular.module('myApp.view1', ['ngRoute']) 

.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/view1', { 
    templateUrl: 'view1/view1.html', 
    controller: 'View1Ctrl' 
    }); 
}]) 

.controller('View1Ctrl', ['$scope', function ($scope) { 

    $scope.data = 'none'; 
    $scope.add = function() { 
     var f = document.getElementById('file').files[0], 
      r = new FileReader(); 
     r.onloadend = function(e) { 
      $scope.data = e.target.result; 
     } 
     r.readAsArrayBuffer(f); 
    } 

}]); 

view1_test.js

'use strict'; 

describe('myApp.view1 module', function() { 

    beforeEach(module('myApp.view1')); 

    describe('view1 controller', function(){ 

    it('should ....', inject(function($controller, $rootScope) { 
     //spec body 
     // var view1Ctrl = $controller('View1Ctrl'); 
     var $scope = $rootScope.$new(), 
      ctrl = $controller('View1Ctrl', { 
      $scope: $scope 
      // $User: {} 
      }); 
     expect(ctrl).toBeDefined(); 
    })); 

    }); 
}); 

app.js

'use strict'; 

// Declare app level module which depends on views, and components 
angular.module('myApp', [ 
    'ngRoute', 
    'myApp.view1', 
    'myApp.view2', 
    'myApp.version' 
]). 
config(['$routeProvider', function($routeProvider) { 
    $routeProvider.otherwise({redirectTo: '/view1'}); 
}]); 

私は潜在的に間違って行くことができどこかわかりませんか?私はこれにかなりの質問を見て、複数の異なるアプローチを試みましたが、私はすべてのテストに合格していますが、これを動作させることはできません。

+0

。 –

答えて

0

問題は私のview1.jsファイルの周りにありました。私はPapa Parseライブラリが非常に便利であることを発見しました。ここで

は、オープンソースパパの解析コミュニティから使用されて私の解決策:あなた `view1_test.js`は、ファイルのアップロードに関連したものをテストしていない

Papa.parse(fileInput[0], { 
    complete: function(results) { 
     console.log("Complete!", results.data); 
     $.each(results.data, function(i, el) { 
      var row = $("<tr/>"); 
      row.append($("<td/>").text(i)); 
      $.each(el, function(j, cell) { 
       if (cell !== "") 
        row.append($("<td/>").text(cell)); 
      }); 
      $("#results tbody").append(row); 
     }); 
    } 
}); 
+0

こちらをご覧ください。 http://stackoverflow.com/questions/41586724/angular-and-papaparse-integration –

関連する問題