![](/img/trans.png)
[英]Spring + Angular JS for File upload with other form fields value
[英]Spring rest post request with file and other input fields using angular js
我試圖通過提交文件作為輸入和其他輸入文本字段來調用帖子請求,我的代碼如下 -
Java方面 -
@RequestMapping(value = "upload",consumes = {"multipart/form-data"},
headers={"Content-Type=multipart/form-data"}, produces = {"multipart/form-
data"}, method = RequestMethod.POST)
public AjaxResponseData<String> upload(@RequestBody RegisterModel
registerModel,@RequestParam(value="file_source", required = false)
MultipartFile file) {
}
角邊 -
$scope.upload= function() {
$http({
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'multipart/form-data',
},
data: {"Name": $scope.name,
"address": $scope.address,
"id": $scope.id,
"phoneNumber": $scope.phone,
"faxNumber": $scope.fax,
"email": $scope.email,
"note": $scope.note,
"file": $scope.file_source},
url: '/tps/register/upload'
}) .then(function(response) { }
}
每當我試圖調用其給出不支持的媒體類型時,錯誤代碼為415。
415錯誤代表不支持的媒體類型 :
請求實體具有服務器或資源不支持的媒體類型。
您正在向服務器發送JSON,但您定義了:
'Content-Type': 'multipart/form-data'
將其更改為
'Content-Type': 'application/json'
服務器端相同:
consumes = {"multipart/form-data"}
至:
consumes = {"application/json"}
你可以在我的代碼中使用它來引用此代碼進行文件上傳。 java代碼:
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
public URL uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
/******* Printing all the possible parameter from @RequestParam *************/
System.out.println("*****************************");
System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
System.out.println("file.getContentType()" + file.getContentType());
System.out.println("file.getInputStream() " + file.getInputStream());
System.out.println("file.toString() " + file.toString());
System.out.println("file.getSize() " + file.getSize());
System.out.println("name " + name);
System.out.println("file.getBytes() " + file.getBytes());
System.out.println("file.hashCode() " + file.hashCode());
System.out.println("file.getClass() " + file.getClass());
System.out.println("file.isEmpty() " + file.isEmpty());
/*************Parameters to b pass to s3 bucket put Object **************/
InputStream is = file.getInputStream();
String keyName = file.getOriginalFilename();
}
角度代碼
var form = new FormData();
form.append("file", "image.jpeg");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://url/",
"method": "POST",
"headers": {
"cache-control": "no-cache"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.