表单数据,文件控制不上传

时间:2018-05-04 07:46:37

标签: spring-boot angular4-forms

我正在尝试使用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;
 }

提交后向我显示以下标题。内容类型未设置。

enter image description here

1 个答案:

答案 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');
});