用blob下载Angular 2中的xlsx文件 [英] downloading xlsx file in angular 2 with blob

查看:288
本文介绍了用blob下载Angular 2中的xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用rest api在angular 2上从客户端下载xlsx文件.

I want to download xlsx file from the client on angular 2 using rest api.

我从GET请求中得到一个字节数组作为响应,并将其发送给带Subscribe的下载功能:

I'm getting a byte array as a response from my GET request and I'm sending it to download function with subscribe:

let options = new RequestOptions({ search: params });
this.http.get(this.restUrl, options)
          .subscribe(this.download); 

使用blob下载功能:

download function using blob:

download(res: Response) {
let data = new Blob([res.arrayBuffer()], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
FileSaver.saveAs(data, this.fileName);};

我的问题是我的文件一直都在损坏. 我尝试了很多版本,但无济于事.

my problem is that my file is corrupted all the time. I tried a lot of versions of this but nothing works.

**也使用此解决方案进行了尝试,但无法正常工作(xlsx文件仍已损坏)- Angular 2下载文件:损坏的结果,不同之处在于我的数据是数组缓冲区而不是字符串或json,并且PDF和xlsx之间是有区别的.

** also tried it with this solution and it doesn't works (the xlsx file is still corrupted) - Angular 2 downloading a file: corrupt result , the difference is that my data is an array Buffer and not string or json, and there is a difference between PDF and xlsx.

10倍!

推荐答案

没有任何效果. 我将服务器更改为返回相同的字节数组,并添加了以下内容:

After nothing works. I changed my server to return the same byte array with the addition of:

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename=deployment-definitions.xlsx");

在我的客户端,我删除了下载功能,而不是我做了的GET部分:

At my client I deleted the download function and instead of the GET part i did:

window.open(this.restUrl, "_blank");

这是我发现可以保存未损坏的xlsx文件的唯一方法.

This is the only way I found it possible to save an xlsx file that is not corrupted.

如果您有关于如何使用Blob的答案,请告诉我:)

If you have a answer about how to do it with blob please tell me :)

这篇关于用blob下载Angular 2中的xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆