【问题标题】:Uploading file with other parameters in spring controller在spring控制器中上传带有其他参数的文件
【发布时间】:2018-06-12 05:18:40
【问题描述】:

我无法将多部分文件和两个文本字段从角度发布请求发送到弹簧控制器。

我的发帖请求如下,

$scope.data = {
            "file": $scope.uploadFile,
            "doucmentType" : $scope.documentType,
            "otherDocumentType" : $scope.otherDocumentType
    }

其中 $scope.uploadFile 是文件对象,doucmnetType 和 otherDocumentType 是我希望与文件一起发送到 spring 控制器的两个文本字段。

post请求如下,

$http.post(appPath + '/temp/uploadAttachments',JSON.stringify($scope.data) ).success(function(result) {

    });

我的spring控制器如下,

@RequestMapping(value = "uploadAttachments", method = RequestMethod.POST)
@ResponseBody
public String uploadAttachments(@RequestBody ReqParam reqParam) {

    JSONObject ret= new JSONObject();
    ret.put("result", true);
    return ret.toString();

}

其中 ReqParam 是 pojo 类,包含文件的 getter 和 setter 以及其他字段,例如,

private MultipartFile file;

private String doucmentType;

private String otherDocumentType;

public String getOtherDocumentType() {
    return otherDocumentType;
}

public void setOtherDocumentType(String otherDocumentType) {
    this.otherDocumentType = otherDocumentType;
}

public String getDoucmentType() {
    return doucmentType;
}

public void setDoucmentType(String doucmentType) {
    this.doucmentType = doucmentType;
}

public MultipartFile getFile() {
    return file;
}

public void setFile(MultipartFile file) {
    this.file = file;
}

谢谢

【问题讨论】:

    标签: java angularjs spring spring-mvc


    【解决方案1】:

    您使用 JSON 解析和 @ResponseBody 以及您的自定义模型。只需使用FormData(),您需要在春季设置MultipartResolver

    这是给你的示例代码:

    角度

    <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', ['$https:', function ($https:) {
            this.uploadFileToUrl = function(file, uploadUrl){
               var fd = new FormData();
               fd.append('file', file);
    
               $https:.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);
            };
         }]);
    
      </script>
    
    </body>
    

    控制器

    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    public @ResponseBody String uploadFile(MultipartFile file, HttpServletRequest req)
            throws SQLException {
    
        // using file.
    
        return "success";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多