【发布时间】:2017-08-22 14:08:18
【问题描述】:
我有一个 web api 方法,它返回一个包含 PDF 文件的 HttpResponseMessage。该方法如下所示:
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
当我从客户端(用 angularJS 编写)调用这个 api 时,Internet 下载管理器会自动捕获 PDF 文件并想要下载它。由于我为我的项目制定了安全计划,IDM 会自动请求用户名和密码。 有谁知道我应该如何以编程方式阻止 IDM 捕获 PDF 文件?
更新:这是我的 angularJS 代码:
$http.post(url, { transactionId: txId }
, {responseType: 'arraybuffer'})
.success(function (response) {
var reader = new FileReader();
var file = new Blob([response.data], {type: 'application/pdf'});
reader.onload = function (e) {
var printElem = angular.element('#printPdfLink');
printElem.attr('target', '_blank');
printElem.attr('href', reader.result);
printElem.attr('ng-click', '');
};
reader.readAsDataURL(file);
})
.error(function (error) {});
【问题讨论】:
-
如果@Nkosi 的回答对您的问题不起作用,我认为您应该将您的Web api 地址放入IDM 配置中的
"Don't start downloading automatically from the following sites:"或...addresses(选项-文件类型) -
你能发布你的 Angular 代码来调用 api 吗?
-
@Kalyan 更新了问题
-
你能把mcve 放在 plunker 或其他地方吗...我想知道代码是否真的触发了下载?
标签: c# angularjs pdf asp.net-web-api