简体   繁体   中英

FileUpload using angular js

I am new to angular js. I am trying to build a functionality to upload an excel sheet from angular UI and pass its data to an REST API to process it further. Please find below my code

Dashboard.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app="msaApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/angular.js"></script>
<script type="text/javascript" src="../js/script.js"></script>
<title>Dashboard</title>
</head>
<body background="../images/bckgd.jpg" >
<div class="header"> 
<table style="width: 100%; height: 10%; "><tr style="height: 142px; ">
<td align="left" style="width: 20%;  "><img src="../images/infosys-bte-logo-GIF.gif" style="width: 280px; height: 54px"></td>
<td align="center" style="width: 80%;  "><h1>MSA Complaince monitoring portal</h1></td>

</tr></table>
</div>
{{2+2}}
<div ng-controller="fileController">
<table align="center">
<tr><td>Please choose a file</td></tr>
<tr><td><input type="file" name="uploadFile" file-input="files"></td><td><button ng-click="fileChanged()">Upload me </button></td></tr>
</table>
</div>

</body>
</html>

Script.js

var msaApp1 = angular.module("msaApp",[]);

msaApp1.directive('fileInput',['$parse',function($parse)
    {

    return{
        restrict:'A',
        link:function(scope,elm,attrs)
        {
            alert(elm);
            elm.bind('change',function()
                    {
                        $parse(attrs.fileInput)
                        .assign(scope,elm[0].files)
                        scope.$apply()
                    })
        }

    }
    }]).controller('fileController' , ['$scope','$http', function($scope,$http)
        {
        $scope.fileChanged=function(elm)
        {
            alert(elm);
            $scope.files=elm.files
            $scope.apply();
            alert($scope.files);
        }
        }])

I am trying to get file object in alert but its giving me below error.

TypeError: Cannot read property 'files' of undefined
    at ChildScope.$scope.fileChanged (script.js:25)
    at fn (eval at compile (angular.js:15156), <anonymous>:4:153)
    at callback (angular.js:26744)
    at ChildScope.$eval (angular.js:17972)
    at ChildScope.$apply (angular.js:18072)
    at HTMLButtonElement.<anonymous> (angular.js:26749)
    at defaultHandlerWrapper (angular.js:3613)
    at HTMLButtonElement.eventHandler (angular.js:3601)

I don't know JSP so I can put a code in simple angular with HTML. It may helpful to resolve your issue. I can try to guide you to resolve an issue. Please review this may helpful to you.

HTML

<html ng-app="fUploadApp">
    <head><title>File upload</title></head>
    <body>
        <div ng-controller = "myCtrl">
            <input type = "file" file-model = "myFile"/>
            <button ng-click = "uploadFile()">Upload File</button>
        </div>
        <script src="angular.min.js"></script>
        <script src="app.js"></script>
    </body>
</html>

App.js

 var fUploadApp = angular.module('fUploadApp', []);

 fUploadApp.directive('fileModel', ['$parse', function ($parse) {
    return {
       restrict: 'A',
       link: function(scope, element, attrs) {
          var model = $parse(attrs.fileModel);
          var modelSetter = model.assign;

          element.bind('change', function(){
             scope.$apply(function(){
                modelSetter(scope, element[0].files[0]);
             });
          });
       }
    };
 }]);

 fUploadApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
       var fd = new FormData();
       fd.append('file', file);

       $http.post(uploadUrl, fd, {
          transformRequest: angular.identity,
          headers: {'Content-Type': undefined}
       })

       .success(function(){
       })

       .error(function(){
       });
    }
 }]);

 fUploadApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
    $scope.uploadFile = function(){
       var file = $scope.myFile;
       var uploadUrl = "/savedata";
       fileUpload.uploadFileToUrl(file, uploadUrl);
    };
 }]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM