gpt4 book ai didi

Angular 4.3 : Getting an arraybuffer with new HttpClient

转载 作者:太空狗 更新时间:2023-10-29 17:44:26 25 4
gpt4 key购买 nike

我想换成新的 HttpClient。到目前为止,我处理以下文件下载:

getXlsx (): Observable<any> {
return this.http.get('api/xlsx', {
responseType: ResponseContentType.ArrayBuffer, // set as ArrayBuffer instead of Json
})
.map(res => downloadFile(res, 'application/xlsx', 'export.xlsx'))
.catch(err => handleError(err));
}

export function downloadFile(data: any, type: string, filename: string): string {
const blob = new Blob([data._body], { type });
const url = window.URL.createObjectURL(blob);

// create hidden dom element (so it works in all browsers)
const a = document.createElement('a');
a.setAttribute('style', 'display:none;');
document.body.appendChild(a);

// create file, attach to hidden element and open hidden element
a.href = url;
a.download = filename;
a.click();
return url;
}

将 respondeType 更改为“arraybuffer”将导致空文件。有什么解决办法吗?

最佳答案

所以 Martin 解决了我的问题:

getXlsx (): Observable<any> {
return this.http.get('api/xlsx', {
responseType: 'blob' // <-- changed to blob
})
.map(res => downloadFile(res, 'application/xlsx', 'export.xlsx'))
.catch(err => handleError(err));
}

export function downloadFile(blob: any, type: string, filename: string): string {
const url = window.URL.createObjectURL(blob); // <-- work with blob directly

// create hidden dom element (so it works in all browsers)
const a = document.createElement('a');
a.setAttribute('style', 'display:none;');
document.body.appendChild(a);

// create file, attach to hidden element and open hidden element
a.href = url;
a.download = filename;
a.click();
return url;
}

关于 Angular 4.3 : Getting an arraybuffer with new HttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46878484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com