我正在使用 Laravel 5.1 REST API 和maatwebsite解决方案,以便在后端生成 Excel 文件。我唯一想要的 - 是在按钮单击时开始下载文件。目前我正在通过本机 Angular2Http
服务发送 AJAX 请求并得到类似的东西
`��ࡱ�;�� ��������������������������������` ...
据我了解,它实际上返回了一个文件,但我必须以正确的方式对其进行解码?
有工作解决方案吗?
我正在使用 Laravel 5.1 REST API 和maatwebsite解决方案,以便在后端生成 Excel 文件。我唯一想要的 - 是在按钮单击时开始下载文件。目前我正在通过本机 Angular2Http
服务发送 AJAX 请求并得到类似的东西
`��ࡱ�;�� ��������������������������������` ...
据我了解,它实际上返回了一个文件,但我必须以正确的方式对其进行解码?
有工作解决方案吗?
在您的服务或您发送 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)})
}