【问题标题】:Why angularJs file upload is failing to send data to spring controller at backend?为什么angularJs文件上传无法将数据发送到后端的spring控制器?
【发布时间】:2016-02-07 09:31:58
【问题描述】:

我正在尝试从前端上传文件以保存在数据库中。但是 Spring 控制器无法接收文件。

HTML

<html>
   <title>FILE_OLPV</title>
   <head>
      <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   </head>

   <body ng-app = "myApp">

      <div ng-controller = "myCtrl">
         <input type = "file" file-model = "myFile"/>
         <button ng-click = "uploadFile()">upload me</button>
      </div>

      <script type="text/javascript" src="js/abc.js"></script>

   </body>
</html>

JS

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

         myApp.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]);
                     });
                  });
               }
            };
         }]);

         myApp.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(){
               });
            }
         }]);

         myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
            $scope.uploadFile = function(){
               var file = $scope.myFile;

               console.log('file is ' );
               console.dir(file);

               var uploadUrl = 'fileUpload';
               fileUpload.uploadFileToUrl(file, uploadUrl);
            };
         }]);

弹簧控制器

@RequestMapping(value="/fileUpload", method=RequestMethod.POST)
public @ResponseBody String upload(@RequestParam("file") MultipartFile file ) throws IOException {
        System.out.println("HEEEEEEEEEEEEEEEEEEEE");

            MultipartFile file1 = file;
            System.out.println(file1);
            System.out.println("Inside for loop");
}

Configuaration.xml

<bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000"/>
</bean> 

无法到达 Spring 控制器。对此有什么想法吗?

【问题讨论】:

  • headers: {'Content-Type': undefined} 是故意的吗?
  • @AdityaSantoso 是的,这是必需的。

标签: java angularjs spring spring-mvc


【解决方案1】:

尝试将@RequestParam("file") 替换为@RequestBody("file")。另请注意,表单数据对象不适用于 IE9 及更早版本

【讨论】:

  • 如果替换为 public @ResponseBody String upload(@RequestBody MultipartFile file ),文件内容为空。我在调试模式下检查了
  • 还将这些属性添加到 Angular http 对象中:enctype: 'multipart/form-data' ,contentType: false, ,processData: false
  • 你的意思是这样说? headers: {'Content-Type': 'multipart/form-data' ,contentType: false, processData: false} 我试过了,但还是空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 2023-04-06
  • 2015-07-23
  • 1970-01-01
  • 2018-05-04
  • 2017-02-07
相关资源
最近更新 更多