请求上传文件:
// upload file to the pre-signed url
const httpOptions = {
headers: new HttpHeaders({
'Content-Disposition': 'attachment;filename=' + file.name + '',
observe: 'response'
})
};
this.httpClient.put(preSingedUrl, file, httpOptions)
.subscribe((response: HttpResponse<any>) => {
console.log(response); // it's returning null
});
响应始终为空,但在网络选项卡中状态为 200 OK。
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-origin: *
content-length: 1207
content-type: application/json
date: Tue, 25 Jun 2019 07:38:06 GMT
status: 200
strict-transport-security: 'max-age=31536000'
x-amz-apigw-id: someid
x-amzn-requestid: someid
x-amzn-trace-id: Root=someid;Sampled=1
如何使用 Angular 8 读取状态 200 OK。
问题在于http选项,这就是为什么您得到响应 null 的原因。
如果您尝试使用观察:“响应”要在订阅中返回响应,您将其错误地作为header.
它应该作为 httpOptions 的属性传递。
为了确认这一点,在下图中,执行httpClient.put()在
类型文件http.d.ts包装内@角度/常见可以看出,其中observe是一个属性。
另外,在类型文件中http.d.ts观察的类型是字符串文字类型。
(有关字符串文字类型的更多信息,请参见此处:https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types).
So observe in http选项需要转换为这种类型。
所以改变这个:
// upload file to the pre-signed url
const httpOptions = {
headers: new HttpHeaders({
'Content-Disposition': 'attachment;filename=' + file.name + '',
observe: 'response'
})
};
对此:
// Custom type for casting
type bodyType = 'body';
// upload file to the pre-signed url
const httpOptions = {
headers: new HttpHeaders({
'Content-Disposition': 'attachment;filename=' + file.name + '',
}),
observe: <bodyType>'response'
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)