Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用blob下载angular 2中的xlsx文件_Angular_Typescript_Blob - Fatal编程技术网

使用blob下载angular 2中的xlsx文件

使用blob下载angular 2中的xlsx文件,angular,typescript,blob,Angular,Typescript,Blob,我想使用RESTAPI从angular 2上的客户端下载xlsx文件 我从GET请求中得到一个字节数组作为响应,并将其发送到带有subscribe的下载函数: let options = new RequestOptions({ search: params }); this.http.get(this.restUrl, options) .subscribe(this.download); 使用blob下载函数: download(res: Response) { le

我想使用RESTAPI从angular 2上的客户端下载xlsx文件

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

let options = new RequestOptions({ search: params });
this.http.get(this.restUrl, options)
          .subscribe(this.download); 
使用blob下载函数:

download(res: Response) {
let data = new Blob([res.arrayBuffer()], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
FileSaver.saveAs(data, this.fileName);};
我的问题是我的文件一直被破坏。 我尝试了很多版本,但都不管用

**还尝试了此解决方案,但不起作用(xlsx文件仍然损坏)- ,不同之处在于我的数据是数组缓冲区,而不是字符串或json,PDF和xlsx之间存在差异

10倍

什么都不起作用。 我将服务器更改为返回相同的字节数组,添加了:

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename=deployment-definitions.xlsx");
在我的客户端,我删除了下载功能,而不是GET部分:

window.open(this.restUrl, "_blank");
这是我发现可以保存未损坏的xlsx文件的唯一方法


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

以下解决方案在Google Chrome 58.0.3029.110版(64位)中对我有效

这是一篇解释如何使用Blob下载pdf的文章:

这与xlsx文件的原理相同。只需按照文章中的步骤进行操作,并更改两件事:

  • 头,而不是{'Accept':'application/pdf'},使用{'Accept':'application/vnd.openxmlformats officedocument.spreadsheetml.sheet'}
  • Blob,使用相同的类型创建Blob,即新Blob([fileBlob],{type:'application/vnd.openxmlformats officedocument.spreadsheetml.sheet'})

这篇文章正在使用文件保护程序,但我没有。我在文章中留下了一条评论,对我用什么代替文件保护程序作了基本解释。

我遇到了与您相同的问题-通过在我的Get选项中添加
responseType:ResponseContentType.Blob
修复了这个问题。检查以下代码:

public getReport(filters: ReportOptions) {
    return this.http.get(this.url, {
            headers: this.headers,
            params: filters,
            responseType: ResponseContentType.Blob
        })
        .toPromise()
        .then(response => this.saveAsBlob(response))
        .catch(error => this.handleError(error));
}
saveAsBlob()只是FileSaver.SaveAs()的包装:


您可以使用以下代码:

var file = new Blob([data], { 
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
});
saveAs(file,"nameFile"+".xlsx");
deferred.resolve(data);

以下是支持IE和chrome/safari浏览器的解决方案。这里的“response”对象是从服务接收到的响应。我把这个存档了。您可以根据从服务接收到的响应更改类型

    let blob = new Blob([response], {type: 'application/zip'});
    let fileUrl = window.URL.createObjectURL(blob);
    if (window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileUrl.split(':')[1] + '.zip');
    } else {
        this.reportDownloadName = fileUrl;
        window.open(fileUrl);
    }

对我来说,问题是,我的回应类型是“文本”。相反,我需要使用“blob”;它在Angular 7.2.15上对我有效。

可能的重复对我有效:这是我最后不得不做的事情。我试图使用从blob创建的url,但浏览器无法确定如何打开该文件。我尝试了这篇文章中的所有方法,还有许多其他方法。您提供的解决方案非常有效。谢谢向你们致敬,你们保存了我的一天@reutUsing上面的方法如何将数据从客户端传递到服务器以加载特定的数据。你能帮我工作吗!我的问题中缺少的关键部分是设置
responseType:'blob'
(使用axios),甚至不需要
const文件=…
。刚做了一个
blob
,然后做了
FileSaver.saveAs(blob,'report.xlsx')
Hi这也适用于我,但我有更改
responseType:'blob'
,这两个文件
.csv
.xlsx
类型文件都适用。请找到我的代码<代码>返回this.http.get(url,{headers:headers,responseType:'blob',observe:'response'})
    let blob = new Blob([response], {type: 'application/zip'});
    let fileUrl = window.URL.createObjectURL(blob);
    if (window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileUrl.split(':')[1] + '.zip');
    } else {
        this.reportDownloadName = fileUrl;
        window.open(fileUrl);
    }