Falha ao carregar o documento PDF - Angular JS - BLOB
Estou tentando obter um documento PDF da API da Web e quero mostrar no aplicativo Angular. Obtendo "Erro ao carregar o documento PDF". Eu segui "AngularJS: Exibir blob (.pdf) em um aplicativo angular"post. Considerando que eu posso baixar o mesmo arquivo com sucesso seguindo"Baixar arquivo de um método de API da Web do ASP.NET usando AngularJS"post.
Parece que estou recebendo o arquivo como "Chunked Transfer Encoded". De alguma forma, isso não está sendo decodificado ao tentar mostrar no aplicativo angular. Por favor informar.
Código da API da Web:
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;
Controlador angular:
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);
});
});
Modelo HTML:
<embed ng-src="{{content}}" style="width:200px;height:200px;"></embed>