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

编程入门 行业动态 更新时间:2024-10-10 09:19:04
本文介绍了Angular 5 下载带有 post 请求的 excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am facing an issue where I have downloaded an Excel file with Angular 1 but if I am implementing the same code in Angular 5 it is showing the error that your file is corrupted. My response is in ArrayBuffer and I am unable to read the file.

下面是我的代码:

服务:

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(); }

我能够下载文件,但无法读取其内容.错误是:

I am able to download the file, but unable to read its content. The error is:

微软 ExcelExcel 无法打开文件███████ DASHBOARD (5).xlsx",因为文件格式或文件扩展名无效.验证文件未损坏并且文件扩展名与文件格式匹配.好的

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 如下:

I struggle with this one all day. Replace angular HttpClient and use XMLHttpRequest as follows:

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 的创建:

Then modified the creation of the Blob in your downloadFile function:

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

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

In your case the service will look something like this:

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 下载带有 post 请求的 excel 文件

本文发布于:2023-10-12 04:23:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Angular   post   excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!