klabautermann123
4/26/2017 - 8:42 PM

WebAPI GetPDF

WebAPI GetPDF

 app.controller('myCtrl', function ($scope,$http, $window) {
        $scope.GetPdf = function () {
            $window.open("/Api/Pdf/GetPdf");
        };
    });
 public HttpResponseMessage GetPdf()
        {

            string path = System.Web.Hosting.HostingEnvironment.MapPath("~/MyPdf.PDF");
            
            byte[] pdf =  System.IO.File.ReadAllBytes(path);
            
            HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(pdf);
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
            result.Content.Headers.ContentDisposition.FileName = "MyPdf.pdf";
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            
            return result;

        }