我试图从Web API获取PDF文档,并希望在Angular App中显示.获取"无法加载PDF文档错误".我在" 角度应用程序 "帖子中跟随了" AngularJS:显示blob(.pdf) ".然而,我可以通过以下" 使用AngularJS从ASP.NET Web API方法下载文件 "文章成功下载相同的文件.
看起来我得到的文件是"Chunked Transfer Encoded".不知何故,当试图在角度应用程序中显示时,这不会被解码.请指教.
Web API代码:
HttpResponseMessage result = null;
var localFilePath = @"C:\Test.pdf";
if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{// serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition.FileName = "Test.pdf";
result.Content.Headers.Add("x-filename", "Test.pdf");
}
return result;
Run Code Online (Sandbox Code Playgroud)
角度控制器:
myModule.controller("pdfviewerController", function ($scope, $http, $log, $sce) {
$http.post('/api/Sample/GetTestFile', {responseType:'arraybuffer'})
.success(function (response) {
var file …Run Code Online (Sandbox Code Playgroud)