在這里,我使用 rxjs Ajax 進行 post api 呼叫并從回應標頭中獲取檔案名并下載檔案。我需要用 angulars http 服務替換 Ajax 呼叫。我不知道如何用 http 替換它,因為我得到的 https 回應中沒有 xhr。在將此 ajax 呼叫轉換為 http 呼叫時需要幫助。謝謝 :)
ajax({
url: url,
method: method,
responseType: "blob",
headers: headers,
body: body,
}).subscribe((data) => {
let filename = "";
let disposition = data.xhr.getResponseHeader("Content-Disposition");
if (disposition && disposition.indexOf("attachment") !== -1) {
let filenameRegex =
/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/;
let matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) {
filename = decodeURIComponent(matches[1]);
}
}
let blob = new Blob([data.response]);
let link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = filename;
link.click();
});
}
uj5u.com熱心網友回復:
http
.get<any>('url', {observe: 'response'})
.subscribe(resp => {
console.log(resp.headers.get('X-Token'));
});
來自這個答案:從 API 回應讀取回應標頭 - Angular 5 TypeScript
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460115.html
標籤:有角度的 阿贾克斯 http 斑点 httpclient
上一篇:根據陣列中的位置重復字符 1
