Javascript 如何将angularjs中的多部分文件传递给rest服务?

Javascript 如何将angularjs中的多部分文件传递给rest服务?,javascript,angularjs,web-services,rest,multipartform-data,Javascript,Angularjs,Web Services,Rest,Multipartform Data,它给出了所需的错误MultipartFile参数“file”不存在…客户端发送的请求在语法上不正确 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> </head> <body ng-app="myApp"> <div ng-controlle

它给出了所需的错误MultipartFile参数“file”不存在…客户端发送的请求在语法上不正确

<html>

<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>
    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/save";
          fileUpload.uploadFileToUrl(file, uploadUrl);
        };
      }
    ]);
  </script>

</body>

</html>

这是web服务。但它无法选择文件

,这可能会对您有所帮助。在输入字段中使用name属性并获取文件。
@Controller
@RequestMapping(value = "/fileUpload")
public class FileUploadController {

    @RequestMapping(value = "/save", method = RequestMethod.POST)
     public @ResponseBody String singleSave(@RequestParam("file") MultipartFile file ){
            String fileName = null;
            String filePath=null;

            if (!file.isEmpty()) {
                try {
                     fileName = file.getOriginalFilename();
                    filePath="C:/cp/" + fileName;

                    byte[] bytes = file.getBytes();
                    BufferedOutputStream buffStream = 
                            new BufferedOutputStream(new FileOutputStream(new File("C:/cp/" + fileName)));
                    buffStream.write(bytes);
                    buffStream.close();

                    ExcelConverter x= new ExcelConverter();
                    x.convertFile(filePath);
                    return "You have successfully uploaded " + fileName;


                } catch (Exception e) {
                    e.printStackTrace();
                    return "You failed to upload " + fileName + ": " + e.getMessage();
                }
            } else {
                return "Unable to upload. File is empty.";
            }
        }

}