Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

How do I build a pdf file/blob from byte[] via a AJAX / REST call

$
0
0
Question: I have WebMethod as illustrated below that should return a byte[]. The byte[] then needs to be retrieved by AJAX call and display the pdf file.
[WebMethod]public static byte[] GeneratePDF() {    var pdfPath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/PDFTemplates/W9.pdf"));    PdfReader pdfReader = new PdfReader(pdfPath);    MemoryStream stream = new MemoryStream();    PdfStamper stamper = new PdfStamper(pdfReader, stream);    pdfReader.Close();    stamper.Close();    stream.Flush();    stream.Close();        byte[] bytes = System.IO.File.ReadAllBytes(pdfPath);        return bytes;             }
Note: for AJAX call, I have following AngularJS code
function GeneratePDF() {$http.post('/Default.aspx/GeneratePDF', '', { responseType: 'arraybuffer' }).then(function (response) { var blob = new Blob([new Uint8Array(response.data)], { type: 'application/pdf' }); $scope.url = (window.URL || window.webkitURL).createObjectURL(blob); window.open($scope.url); }); }
And html anchor below for downloading pdf file

Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>