(角度 8) 在 $http Post 数据中发送文件数据会在 rails API 日志上给出 {}



angular 8, Rails 5.2 API

我正在尝试以编辑形式将图像附加到我的Event模型中。我正在遵循本教程

我的打字稿代码:

const httpOptions = {
headers: new HttpHeaders({
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8; multipart/form-data',
Authorization: environment.apiAuthKey,
})
};
updateEvent(event: Event): Observable<Event> {
return this.http.put<Event>(url, event, httpOptions)
.pipe(
...
}

这会在 Rails API 端生成这些日志:

Started PUT ...
Processing by ... as JSON
Parameters: {"title"=>"testing image", "image" => {}}
...

知道为什么图像参数为空吗?

到目前为止,从我的研究中,我有一个想法,我必须将多部分表单数据放在我的HTTP标头中,我这样做了,但输出没有变化。

尝试使用formData而不是FormGroup

const formData = new FormData();
formData.append('image', image);
formData.append('title', testing image);
this.http.post(url, formData);

您也可以在不设置HttpHeaders 的情况下进行测试发布,FormData 将为您设置合适的标头

最新更新