我正在try 将角形数据上传到Spring Boot服务器.
简单地说,在《春靴》中我接受:
data class Photo(
val id: Long = 0L,
val file: MultipartFile
)
data class PostRequest(
@field:Size(min = 1, max = 100)
val title: String,
val photos: List<Photo> = mutableListOf()
)
通过控制器:
fun createPost(@Valid @ModelAttribute postRequest: PostRequest)
在Angular 上,我创建了这个形状:
this.postForm = this.fb.group({
title: ["", [Validators.required, Validators.minLength(1)]],
photos: this.fb.array([]),
});
当用户上传照片时,我将这些线路称为:
const photosArray = this.postForm.get("photos") as FormArray;
const newPhotoGroup = this.fb.group({
id: "",
file: event.target.result,
});
photosArray.push(newPhotoGroup);
最后,我构造了一个表单数据对象:
const formData = new FormData();
formData.append("title", this.postForm.get("title")?.value);
formData.append("photos", this.postForm.get("photos")?.value);
我使用此函数将此表单发布到API:
postRequest<T>(url: string, body: any): Observable<HttpResponse<T>> {
const headers = new HttpHeaders({
"Content-Type": "multipart/form-data",
});
return this.http.post<T>(url, body, {
headers,
observe: "response",
withCredentials: true,
});
}
在开发人员工具中,我看到以下请求:
Spring Boot抱怨道:
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
谁来给我开导一下.谢谢.