我有一个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) 我正在尝试使用web api 2和angular js下载csv文件.
这是我的控制器代码
public IHttpActionResult ExportCsvData()
{
var stream = new FileStream("Testcsv.csv", FileMode.Open, FileAccess.Read);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Testcsv.csv"
};
return Ok(response);
}
Run Code Online (Sandbox Code Playgroud)
这是我的角度代码,
var filename = 'testcsv.csv';
var contentType = 'text/csv;charset=utf-8';
var blob = new Blob([data], { type: contentType });
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用IE 11,但是当我在excel中打开文件时,它看起来像这样,
{ "version": {
"major": 1
"minor": 1
"build": -1
"revision": -1
"majorRevision": -1 …Run Code Online (Sandbox Code Playgroud)