gpt4 book ai didi

Angular 5下载带有发布请求的excel文件

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

我遇到一个问题,我用 Angular 1 下载了一个 Excel 文件,但如果我在 Angular 5 中实现相同的代码,它会显示文件已损坏的错误。我的响应在 ArrayBuffer 中,但我无法读取该文件。

下面是我的代码:

服务:

 DownloadData(model:requiredParams):Observable<any>{
const headers = new Headers();
const requestOptions = new RequestOptions({ headers: headers });
requestOptions.headers.append('Content-Type', 'application/json');

const body = JSON.stringify(model);
return this.http.post(url, body, requestOptions)
.map((res:any) => res)
.catch((e: any) => Observable.throw(this.errorHandler(e)));
}

组件:

exportToExcel() {
this.loadingOverlayFlag = true;
this.podashboardService.DownloadData(this.data).subscribe(result=>{
console.log(result);
this.downloadFile(result._body,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'export.xlsx');
})
}

downloadFile(blob: any, type: string, filename: string) {

var binaryData = [];
binaryData.push(blob);

const url = window.URL.createObjectURL(new Blob(binaryData, {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})); // <-- 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();

}

我可以下载文件,但无法读取其内容。错误是:

Microsoft Excel
Excel cannot open the file '███████ DASHBOARD (5).xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file. OK

最佳答案

我整天都在为这个问题苦苦挣扎。替换 angular HttpClient 并使用 XMLHttpRequest,如下所示:

var oReq = new XMLHttpRequest();
oReq.open("POST", url, true);
oReq.setRequestHeader("content-type", "application/json");
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response;
if (arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
console.log(byteArray, byteArray.length);
this.downloadFile(byteArray, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'export.xlsx');
}
};

oReq.send(body);

然后在 downloadFile 函数中修改 Blob 的创建:

const url = window.URL.createObjectURL(new Blob([binaryData]));

在您的情况下,服务将如下所示:

DownloadData(model:requiredParams):Observable<any>{
return new Observable(obs => {
var oReq = new XMLHttpRequest();
oReq.open("POST", url, true);
oReq.setRequestHeader("content-type", "application/json");
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response;
var byteArray = new Uint8Array(arrayBuffer);
obs.next(byteArray);
};

const body = JSON.stringify(model);
oReq.send(body);
});
}

然后是组件:

exportToExcel() {
this.loadingOverlayFlag = true;
this.podashboardService.DownloadData(this.data).subscribe(result=>{
// console.log(result);
this.downloadFile(result,'application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet', 'export.xlsx');
})
}

downloadFile(blob: any, type: string, filename: string) {

var binaryData = [];
binaryData.push(blob);

const url = window.URL.createObjectURL(new Blob(binaryData, { type: filetype })); // <-- 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();
}

关于Angular 5下载带有发布请求的excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49422518/

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