问题描述
我正在使用以下函数将文件上传到带有 Angular 7 的 HttpClient 的服务器
pushFileToStorage(productId, key, file: File): Observable知道我做错了什么吗?谢谢
解决方案 我刚刚发现我正在处理的项目有一个 HttpInterceptor,它正在添加内容类型application/json";默认情况下,任何未设置内容类型的请求.这就是问题所在.
I'm using the following function to upload a file to a server with the HttpClient of angular 7
pushFileToStorage(productId, key, file: File): Observable<any> {
let formdata: FormData = new FormData();
formdata.append('prod', file);
let url_ = '/admin5/api/v1/product/images/upload?';
url_ += "productId=" + encodeURIComponent("" + productId) + "&";
url_ += "kind=" + encodeURIComponent("" + key);
return this.http.post(url_,formdata);
}
The problem I'm having is that the HttpClient sets the wrong content type header (application/json instead of "multipart/form-data") and so the server can't read the file.This is what I see on the developer tools
Any idea on what I'm doing wrong?Thanks
解决方案 I just found out that the project I'm working on has an HttpInterceptor that is adding a content-type "application/json" by default to any request that doesn't set the content type. So that was the problem.
这篇关于具有多部分表单的 Angular 后请求具有错误的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!