2017-05-01 78 views
0

我知道這個問題已經被問了個遍,還嘗試所有可能的選項後,但我得到這個錯誤:「當前請求不是多要求」 angularjs春季啓動

the current request is not a multipart

這裏是我的代碼角:

app.controller("controller", function ($scope, $http, $window, $log) { 

    $scope.upload = function() { 
     var file = $scope.myFile; 
     var fd = new FormData(); 
     fd.append('file', file); 
     $http.post('upload', fd, { 
      transformRequest: angular.identity, 

     }) 
      .success(function(){ 
      }) 
      .error(function(){ 
      }); 
    }; 

控制器代碼:

@RequestMapping(value = "/upload", method = RequestMethod.POST) 
    public Callable<FileUpload> uploadFile(@RequestParam("file") MultipartFile file) throws Exception { 
     parser = fileHandlerService.getParser(file); 
     Callable<FileUpload> asyncResult = new Callable<FileUpload>() { 
      @Override 
      public FileUpload call() throws Exception { 
       FileUpload fileUpload = fileHandlerService.getUploadStatus(); 
       return fileUpload; 
      } 
     }; 

     return asyncResult; 
    } 

HTML代碼:

<div class="vertical-center"> 
     <input type="file" file-model="myFile"> 
     <button ng-click="upload()" class="btn btn-primary btn-lg">Upload 
     </button> 
</div> 

我試圖把:headers: {'Content-Type': undefined }但是與添加標題,我沒有得到錯誤500了,但我得到錯誤400

我送過Postman文件沒有任何標題,它工作正常。所以我不明白爲什麼它不通過角度。

在此先感謝。

回答

0

除了添加標題:{'Content-Type':undefined},您可以將div更改爲表單並將enctype =「multipart/form-data」添加到表單中。即你的代碼看起來像;

<form class="vertical-center" enctype="multipart/form-data"> 
    <input type="file" file-model="myFile"> 
    <button ng-click="upload()" class="btn btn-primary btn-lg">Upload 
    </button> 
</form>