问题描述
我正在尝试使用以下Angular Code下载文件,但我一直在获取需要参数“键”
const headerValues = new HttpHeaders({ 'Content-Type': contentType!,'Accept': contentType! });
this.http
.get(`${this.resourceUrl}/${id}/download`,{ headers: headerValues })
.subscribe(data => {
const blob = new Blob([data as BlobPart],{ type: contentType });
const downloadUrl = window.URL.createObjectURL(blob);
window.open(downloadUrl);
console.log('Alert :: Done ');
},(error) => {
console.log('Alert :: error: ' + error);
},() => {
console.log("this is the finally block");
});
它一直在打印警报::标签错误错误:需要参数“键”。找不到任何相关内容。 TranslateService会抛出此错误,但我不知道为什么
解决方法
我找到了解决问题的方法。默认responseType为blob |未定义。在this.http.get()选项参数中添加blob responseType解决了该问题。
this.http.get(`${this.resourceUrl}/${id}/download`,{ headers,responseType: 'blob' }).subscribe((res) => {
const file = new Blob([res as BlobPart],{
type: contentType,});