我在AngularJS中完全虚拟,并感谢你的帮助。
我正在尝试实施文件上传页面。它非常简单,用户选择文件并按下上传按钮,它必须上传到后端,这是一个Spring MVC REST Web服务。我试图使用ng-file-upload。
这是我的前端代码:
<form name="myForm">
<input type="file" ngf-select ng-model="picFile" name="file
accept="application/pdf" ngf-max-size="2MB" required
ngf-model-invalid="errorFiles">
<i ng-show="myForm.file.$error.required">*required</i><br>
<i ng-show="myForm.file.$error.maxSize">File too large
{{errorFiles[0].size / 1000000|number:1}}MB: max 2M</i>
<button ng-disabled="!myForm.$valid" ng-click="uploadFiles(picFile)">Submit</button>
和控制器:
$scope.uploadFiles = function(file, errFiles) {
console.log("Uploading files");
$scope.f = file;
$scope.errFile = errFiles && errFiles[0];
if (file) {
file.upload = Upload.upload({
url: 'api/proposals/upload',
data: {file: file}
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
file.progress = Math.min(100, parseInt(100.0 *
evt.loaded / evt.total));
});
}
};
和我的后端:
@RequestMapping(value = "/proposals/upload",method = RequestMethod.POST)
@Timed
public ResponseEntity<TemporaryProposal>
uploadProposal(@RequestParam("file") MultipartFile agreementFile){
我在IE9控制台中遇到的错误是
TypeError: Unable to get value of the property 'error': object is null or undefined
从后端:
[DEBUG] com.iocs.portal.aop.logging.LoggingAspect - Enter: org.springframework.boot.actuate.audit.AuditEventRepository.add() with argument[s] = [AuditEvent [timestamp=Tue Mar 01 08:53:23 EST 2016, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={message=Access is denied, type=org.springframework.security.access.AccessDeniedException}]]
[DEBUG] com.iocs.portal.aop.logging.LoggingAspect - Exit: org.springframework.boot.actuate.audit.AuditEventRepository.add() with result = null
[DEBUG] com.iocs.portal.security.Http401UnauthorizedEntryPoint - Pre-authenticated entry point called. Rejecting access
答案 0 :(得分:0)
我通过从前端发送身份验证参数和请求来解决此问题。当我使用Auth2身份验证时,可以在请求中包含令牌。