Failed to load PDF document - Angular JS - BLOB

Mahesh Pulle

I am trying to get PDF document from Web API, and want to show in Angular App. Getting "Failed to load PDF document error". I have followed "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" post.

Looks like i am getting the file as "Chunked Transfer Encoded". Somehow this is not getting decoded when trying to show in angular app. Please advise.

Web API Code:

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;

Angular Controller:

   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 Template:

<embed ng-src="{{content}}" style="width:200px;height:200px;"></embed>
Mahesh Pulle

problem is there in controller. {responseType:'arraybuffer'} is verymuch required. For $http.get - It should be second parameter. For $http.post - It should be third parameter.

In above case, i am using $http.post and i have passed {responseType:'arraybuffer'} as second parameter.

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

Corrected code

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

'Error Failed to Load PDF Document' when Returning PDF From Azure Blob Storage

From Dev

Failed to load Pdf Document html-pdf

From Dev

Failed to load PDF document in chrome browser

From Dev

Failed to load PDF document in my PHP code

From Dev

pdf creation shows an error "failed to load pdf document" in zend

From Dev

Failed to load module in Angular JS

From Dev

PHP Download script error failed to load PDF document

From Dev

Failed to load document PHPMailer PDF Base64 string

From Dev

Angular : converting blob into pdf

From Dev

Can't load PDF document

From Dev

Angular 7: ngsw-worker.js failed to load

From Dev

Angular JS, UI-Bootstrap Failed to load template

From Dev

Failed to load .js resource

From Dev

Load Pdf in angular popup

From Dev

Angular and html - Failed to load resource

From Dev

Unable to load pdf from document directory in Swift

From Dev

Azure Blob Storage - upload a document using js

From

Android failed to load JS bundle

From Dev

Failed to load resource in Node JS

From Dev

Gulpfile.js failed to load

From Java

BLOB to PDF download using Java and angular 5

From Dev

Failed to load resource: the server responded with a status of 404 (Not Found) angular js + ionic

From

Angular 2 unit testing - getting error Failed to load 'ng:///DynamicTestModule/module.ngfactory.js'

From

zone.js:344 Unhandled Promise rejection: Failed to load app.template.html in angular2

From Dev

angular-rails-templates Failed to load template

From Dev

Show message if Angular app failed to load / crashed?

From Dev

JS function fire only once on document load

From Dev

Popper.js not initializing on document load

From Dev

THREE.js failed to load json model

Related Related

HotTag

Archive