0
我需要發送圖像到服務器,我使用角2,因此請求必須是異步的。這是我用來發送客戶端上的圖像的代碼:文件不是從異步發送或接收XmlHttpRequest
return Observable_1.Observable.create(function (observer) {
var formData = new FormData();
var xhr = new XMLHttpRequest();
formData.append('image', image);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
observer.next({ response: xhr.response, status: xhr.status });
observer.complete();
}
else {
observer.error({ response: xhr.response, status: xhr.status });
}
}
};
xhr.open('POST', url, true);
if (headers)
for (var _i = 0, headers_1 = headers; _i < headers_1.length; _i++) {
var header = headers_1[_i];
xhr.setRequestHeader(header.header, header.value);
}
xhr.send(formData);
});
我使用喊得服務器上
@PostMapping(value = "/api/product/image", consumes = {"multipart/mixed", "multipart/form-data"})
public ResponseEntity<ImageToken> postImage(MultipartHttpServletRequest request) throws ParseException {
MultipartFile file = request.getFile(request.getFileNames().next());
return imageService.saveImage("/products/", file);
}
的requst的代碼我真的不知道什麼是錯的。該處理程序正在被觸發,但會在request.getFileName()上拋出異常,並且在調試器中不會看到請求中的任何文件。
這裏是我的multipart解析器的conf:
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxUploadSize(500000000);
resolver.setMaxUploadSizePerFile(50000000);
return resolver;
}
我的Apache公地的版本和春季啓動:
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
謝謝,我知道,並已嘗試過,但它不改變請求本身並不包含任何文件的事實,儘管我可以看到它們在開發者工具中被髮送=( –