我有一个REST API,如果我直接在浏览器中查询,它将返回一个JSON或CSV文件进行下载。我想对Angular HttpModule做同样的事情。
我的get请求很简单(它有一个查询字符串参数来指示要返回的文件格式):
public Submit(request: Request) {
...
return this.http.get(url).subscribe(response => {
//What here
});
}
Run Code Online (Sandbox Code Playgroud)
响应内容类型为application/octet-stream。我尝试将响应作为Blob进行处理,但这不起作用。我如何才能将服务器的响应传递到浏览器,然后让其下载文件?此方法是响应按钮单击的组件正在调用的服务的一部分
onClick() {
this._requestService.Submit(request); //This gets the Subscription response from the service, but I want it to let the broswer download the file
}
Run Code Online (Sandbox Code Playgroud)
您必须添加
{ responseType: ResponseContentType.Blob }
Run Code Online (Sandbox Code Playgroud)
然后将响应映射到Blob。
return this.http.get(url, { responseType: ResponseContentType.Blob })
.map(r => r.blob())
.subscribe(response => {
});
Run Code Online (Sandbox Code Playgroud)
接下来,您可以使用fileSaver.js下载此文件。
| 归档时间: |
|
| 查看次数: |
15439 次 |
| 最近记录: |