Angular2:从后台下载xls文件

我正在使用Laravel 5.1 REST API和maatwebsite解决scheme来在后端生成Excel文件。 我想要的唯一的事情就是开始下载button点击文件。 目前我通过本地的Angular2 Http服务发送一个AJAX请求,并得到这样的东西

 `  ࡱ ;                                   ` ... 

据我了解,它实际上返回一个文件,但我必须以适当的方式解码它?

有工作解决scheme吗?

在你的服务或你的组件发送http请求。 将你的回应映射到blob。 从@ angular / http导入ResponseContentType。 对于下载做这样的:

 var link=document.createElement('a'); link.href=window.URL.createObjectURL(res); link.download="District.xls"; link.click(); 

在服务中这样做:

 import { Response, ResponseContentType } from '@angular/http'; downloadExcel(){ let url = 'your url/api' return this.http.get(url,{headers:'pass your header',responseType: ResponseContentType.Blob}) .map(response=>response.blob()) .catch(error=>{return Observable.throw(error)}) }