angular 5 - 具有特定标题的http帖子

问题描述 投票:0回答:1

我的标题在构造函数中设置如下:

this.headers = new Headers();
this.headers.append('Content-Type', 'multipart/form-data;boundary=--boundary');

我已经收集了数据以在formData中发布

            .....
 let data = new FormData();
     data.append('file', file);
     data.append('fileName', file.name);
     data.append('fileSize', file.size.toString());
     data.append('fileType', file.type);
     data.append('fileLastMod', file.lastModifiedDate);
            .....

我在这里发布数据

let url = 'http://api.********.com/gallery/'+ this.selectedCategory;
            this._http.post(url, data, {headers : this.headers})
                .toPromise()
                .catch(reason => {
                    console.log(JSON.stringify(reason));
                }).then(result => {
                console.log('From Promise:', result);
            });

但我得到以下错误:

enter image description here

我正在接近的API需要遵循:

enter image description here


所以我的问题是如何设置这些要求,如边界,内容处置等等?这些对我来说都很新鲜。

javascript angular http-headers
1个回答
2
投票

如果要在请求中发送formdata对象,则不需要导入content-type。

Angular HTTP方法支持多部分类型,其中HttpClient也支持具有默认reportProgress功能的multipart,它将通知上传状态

解决方案 - 使用HTTP方法,它将自己设置边界。

© www.soinside.com 2019 - 2024. All rights reserved.