Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a requirement to open the pdf in window where the pdf is returned from the Web API and show in the AngularJs application.

Thanks for your help

What I have tried:

When I invoke the Web API method directly the PDF is appropriately getting exported, but when I'm trying to handle the export done in AngularJs using the below mentioned code it shows invalid document or corrupted document.

JavaScript
function openPDF(resData, fileName) {
      var ieEDGE = navigator.userAgent.match(/Edge/g);
      var ie = navigator.userAgent.match(/.NET/g); // IE 11+
      var oldIE = navigator.userAgent.match(/MSIE/g);

      var blob = new window.Blob([resData], { type: 'application/pdf' });

      if (ie || oldIE || ieEDGE) {
        window.navigator.msSaveBlob(blob, fileName);
      }
      else {
        var reader = new window.FileReader();
        reader.onloadend = function () {
          window.location.href = reader.result;
          window.target = "_blank";
        };
        reader.readAsDataURL(blob);
      }
    }


Web API response

HttpResponseMessage result = null;
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(bytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "Test.pdf";
return result;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900