данные формы с контролем файлов не загружаются - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь загрузить форму с файлом и данными формы, используя angular4 и весеннюю загрузку. я установил тип контента как 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');
  });

}

Серверная часть

На стороне сервера я использую пружинную загрузку

@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 голосов
/ 07 мая 2018

Используйте следующий код для установки заголовков. Это может вам помочь,

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