2017-02-23 50 views
0

我使用Laravel 5.1 REST API和maatwebsite解决方案,以便在后端生成Excel文件。我想要的唯一的事情就是开始下载按钮点击文件。 目前我送经自然Angular2 Http服务的AJAX请求,像这样Angular2:从后台下载xls文件

`��ࡱ�;�� ��������������������������������` ... 

按照我的理解,它实际上返回一个文件,但我必须把它在一个适当的解码方式得到的东西?

有工作解决方案吗?

回答

1

在您的服务或您的组件发送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)}) 
    }