我正在尝试使用angular4和spring boot上传包含文件和表单数据的表单。我将内容类型设置为multipart/form-data
,但仍然显示400错误。
我的角度是
this.formobj["userID"] = this.userId;
this.formobj["userName"] = this.addfrm.value.userName;
this.formobj["userDescription"] = this.addfrm.value.userDes;
this.formobj["files"] = this.addfrm.value.image;
let type = new HttpHeaders();
type.append('Content-Type', 'multipart/form-data');
this.http.post(this.Core_URL + '/updateUserWithFile', this.formobj, {headers: type})
.subscribe(data => {
console.log("** form submited");
console.log(data);
this.clearShareObj();
this.r.navigateByUrl('home');
});
}
服务器端
服务器端我正在使用spring boot
@PostMapping("/updateUserWithFile")
private FunctionInfo updateUserWithFile(@RequestBody UserInfo userInfo) {
System.out.println("ok");
int userId = userInfo.getUserId();
String userName = userInfo.getUserName();
MultipartFile[] files; = userInfo.getFile();
}
DomainClass
public class UserInfo {
private Integer userID;
private String userName;
private String userDescription;
private MultipartFile[] files;
}
提交后向我显示以下标题。内容类型未设置。
答案 0 :(得分:0)
使用以下代码设置标头。这可能会对你有帮助,
import { Http, Headers } from '@angular/http';
和
constructor(private http: Http){}
在你的方法中使用
const headers = new Headers({'Content-Type', 'multipart/form-data'});
return this.http.post(this.Core_URL + '/updateUserWithFile', this.formobj,
{headers: headers})
.subscribe(data => {
console.log("** form submited");
console.log(data);
this.clearShareObj();
this.r.navigateByUrl('home');
});