2017-02-04 25 views
0

我一直在尋找this,但似乎我的問題在別處。我正在嘗試上傳文件。該input當前被定義爲:MissingServletRequestPartException:所需的請求部分「文件」不存在

<input 
    type="file" 
    style="display: none;" 
    name="file"  
    multiple  
    nv-file-select     
    uploader="uploader"> 

這是如何進行上傳:

var uploader = $scope.uploader = new FileUploader({ 
    url: 'http://localhost:8080/rest-api/dl4j/we/uploadModel' 
}); 

uploader.onAfterAddingFile = function($modelFile) { 

    var fd = new FormData();   
    fd.append('file', $modelFile.file); 

    $http.post($modelFile.url, fd, { 
     headers: { 
      'Content-Type': undefined 
     }, 
     transformRequest: angular.identity   
    }) 
    .then(
     function (data) { 
      alert("upload success"); 
     }, 
     function (data, status) { 
      alert("upload error"); 
     } 
    ); 
}; 

雖然這是Spring REST端點:

@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL) 
@RequestMapping(method=RequestMethod.POST, headers={"Content-Type=multipart/form-data"}) 
public ResponseEntity<WordVectorListDto> uploadModel( 
     @RequestParam("file") MultipartFile file, 
     RedirectAttributes redirectAttributes) { 

    LOGGER.debug("POST uploadModel"); 

    return new ResponseEntity<WordVectorListDto>((WordVectorListDto)null, HttpStatus.OK); 
} 

的問題是雖然,春天拋出異常,告訴我參數file不存在:

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present 

這是請求信息:

enter image description here

我怎樣才能讓這個文件上傳的工作?

+1

你完成這件事?我遇到了與我的Angular2應用程序相同的問題 – vigamage

回答

0

我想這可以與內容類型 在你的代碼中,我看到:

$http.post($modelFile.url, fd, { 
     headers: { 
      'Content-Type': undefined 
     }, 
     transformRequest: angular.identity   
    }) 

所以要定義一個未定義的內容類型;你應該設置multipart/form-data 嘗試把這個內容類型

我希望這是有用的

安傑洛

+0

不,在這種情況下,內容類型設置正確 - 不確定,但我認爲這是通過角度以某種方式完成的。正如您從請求中看到的那樣,Content-Type標頭設置正確。 – displayname

相關問題