我想上传一个文件并将其发布到我的服务器以获取响应。
在模板中,我使用类似以下方式:
<form name="myForm" enctype="multipart/form-data">
<input type="file" ngf-select ng-model="picFile" name="file" accept="image/*" required>
<button class="btn btn-primary" type="submit" ng-click="upload(picFile)" ng-disabled="!myForm.$valid">Ajouter
</form>
这是我的Angular控制器和服务:
$scope.upload = function (files) {
if (files && files.length) {
var file = files[0];
BiAddDocFromExternalSourceService.uploadFile(file);
}
}
// Service:
service.uploadFile = function (file) {
var fd= new FormData();
fd.append('file', file);
return $http.post('ws/bdocument/add_external_document', fd, {
transformRequest: angular.identity,
headers: {
'Content-Type': 'undefined'
}
});
};
在服务器端,我开发了一个类似于这样的REST服务:
@RequestMapping(value = "/add_external_document", method = RequestMethod.POST)
public byte[] retrieveBDocumentThumbnail(@RequestParam MultipartFile file)
throws AbstractBdocInteractiveException {
//do something....
return something;
}
以下是向服务器发送 PUT 请求时如果使用了 **'Content-Type': 'undefined'** 的请求内容:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/bdoci-
tablet/ws/bdocument/add_external_document
Request Method:POST
Status Code:500 Erreur Interne de Servlet
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ar;q=0.2
Connection:keep-alive
Content-Length:13520
Content-Type:undefined
Cookie:JSESSIONID=25BE1D0F0102A5D3465EFC0644494D71; __ngDebug=true
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/bdoci-tablet/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/39.0.2171.95 Safari/537.36
Response Headers
Connection:close
Content-Language:fr
Content-Length:4261
Content-Type:text/html;charset=utf-8
Date:Wed, 20 May 2015 11:02:20 GMT
Server:Apache-Coyote/1.1
并且看起来如果我放置
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/bdoci-
tablet/ws/bdocument/add_external_document
Request Method:POST
Status Code:500 Erreur Interne de Servlet
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ar;q=0.2
Connection:keep-alive
Content-Length:48185
Content-Type:multipart/form-data
Cookie:JSESSIONID=EBDEED7F0EAE46677ABD2B2861FEA2A6; __ngDebug=true
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/bdoci-tablet/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/39.0.2171.95 Safari/537.36
Response Headers
Connection:close
Content-Language:fr
Content-Length:5266
Content-Type:text/html;charset=utf-8
Date:Wed, 20 May 2015 13:12:04 GMT
Server:Apache-Coyote/1.1
尝试这个操作时,会抛出以下异常:
org.springframework.web.multipart.MultipartException:当前请求不是多部分请求
org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为未找到多部分边界
Content-Type:undefined
是否未定义或为多部分。 - Fernando Garcia