Summary of Issues Using HttpClient in Angular
5/26/23Less than 1 minute
Regarding the blob Return Type
Handling situations when a file download is required, the return type is set to Blob, but a backend error occurs and JSON type data is returned
this.http
.post(
`/index/v2/downloadIndexValTemplate`,
{
...this.condition,
},
{ responseType: "blob" }
)
.subscribe((res) => {
if (res.type == "application/vnd.ms-excel") {
const a = document.createElement("a");
a.href = URL.createObjectURL(res);
a.download = "file.xlsx";
a.click();
} else {
const reader = new FileReader();
reader.readAsText(res);
reader.onloadend = () => {
const { reason } = JSON.parse(reader.result + "");
this.message.error(reason);
};
}
});AI Translation | AI 翻译
This article was translated from Chinese to English by AI. If there are any inaccuracies, please refer to the original Chinese version.
本文由 AI 辅助从中文翻译为英文。如遇不准确之处,请以中文原版为准。
