我是棱角分明的新手。我正在尝试构建一个从角度UI上传Excel工作表的功能,并将其数据传递给REST API以进一步处理它。 请在下面找到我的代码
<%@ 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>
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);
}
}])
我正在尝试将文件对象置于警报状态,但它会给我以下错误。
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)
答案 0 :(得分:0)
我不了解JSP所以我可以使用HTML将代码放入简单的角度。解决您的问题可能会有所帮助。我可以尝试指导您解决问题。请查看这可能对您有所帮助。
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);
};
}]);