java pdf无法加载_无法加载PDF文档 - Angular JS - BLOB

长孙波鸿
2023-12-01

我试图从Web API获取PDF文档,并希望在Angular App中显示 . 获得"Failed to load PDF document error" . 我已经关注了“AngularJS: Display blob (.pdf) in an angular app " post. Whereas, i can download the same file successfully by following " Download file from an ASP.NET Web API method using AngularJS”的帖子 .

看起来我得到的文件是“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;

角度控制器:

myModule.controller("pdfviewerController", function ($scope, $http, $log, $sce) {

$http.post('/api/Sample/GetTestFile', {responseType:'arraybuffer'})

.success(function (response) {

var file = new Blob([response], { type: 'application/pdf' });

var fileURL = URL.createObjectURL(file);

$scope.content = $sce.trustAsResourceUrl(fileURL);

});

});

HTML模板:

 类似资料: