Angular正在阻止文件下载

时间:2017-02-02 20:40:18

标签: javascript angularjs asp.net-web-api

当我针对get控制器发起webapi请求时,HttpResponseMessage控制器响应 return $http.get(serviceURLRoot + 'api/myreport/' + dateParams.StartDate + '/' + dateParams.EndDate, { withCredentials: true }).success(function (data) { }); 中包含的文件,浏览器无法识别它文件下载,数据丢失。

我检查了fiddler,我可以看到响应中存在二进制数据。

我用这个来启动get请求:

window.open

如果我直接在地址栏中输入请求,我就可以恢复文件了。使用window.open(serviceURLRoot + 'api/rawdatareport/' + dateParams.StartDate + '/' + dateParams.EndDate); 也可以:

$http.get

为什么HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new ByteArrayContent(pck.GetAsByteArray()); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "myReport.xlsx" }; return result; 无效?

编辑-----

这里是在服务器上构建的HttpResponseMessage:

{{1}}

1 个答案:

答案 0 :(得分:0)

因为您正在使用$ http执行AJAX请求。浏览器实现文件的唯一方法就是让浏览器去那里。实现文件即将到来,而不是HTML,它会弹出下载。因此,您必须将位置更改为该URL或在该URL中打开一个新窗口。