我有一个WebApi/MVC应用程序,我正在开发一个angular2客户端(以取代MVC).我在理解Angular如何保存文件时遇到了一些麻烦.
该请求是确定的(正常工作与MVC,我们可以登录接收到的数据),但我无法弄清楚如何保存下载的数据(我主要是遵循同样的逻辑这篇文章).我确信这是非常简单的,但到目前为止,我根本不理解它.
组件功能的代码如下.我尝试过不同的方案,则斑的方法应该是,据我了解的路要走,但没有作用createObjectURL在URL.我甚至找不到URL窗口中的定义,但显然它存在.如果我使用该FileSaver.js模块,我会得到同样的错误.所以我猜这是最近发生的变化或者尚未实施.如何在A2中触发文件保存?
downloadfile(type: string){
let thefile = {};
this.pservice.downloadfile(this.rundata.name, type)
.subscribe(data => thefile = new Blob([data], { type: "application/octet-stream" }), //console.log(data),
error => console.log("Error downloading the file."),
() => console.log('Completed file download.'));
let url = window.URL.createObjectURL(thefile);
window.open(url);
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,获取数据的服务如下,但它唯一做的是发出请求并传递数据而不进行映射(如果成功):
downloadfile(runname: string, type: string){
return this.authHttp.get( this.files_api + this.title +"/"+ runname + "/?file="+ type)
.catch(this.logAndPassOn);
}
Run Code Online (Sandbox Code Playgroud)