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]
publicstaticbyte[] GeneratePDF()
{
var pdfPath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/PDFTemplates/W9.pdf"));
PdfReader pdfReader = newPdfReader(pdfPath);
MemoryStream stream = newMemoryStream();
PdfStamper stamper = newPdfStamper(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
According to your above description, I think you want to download a PDF file through the ajax and web service. The following link provides a demo about using Iframe to achieve it that you could have a look.
In link provides a demo about first build the contents of PDF through ajax calling web service during page loading and then use Server button to download the PDF, maybe you could have a look.
None
0 Points
14 Posts
How do I build a pdf file/blob from byte[] via a AJAX / REST call
Mar 01, 2016 08:51 PM|EddieFlo|LINK
Member
590 Points
194 Posts
Re: How do I build a pdf file/blob from byte[] via a AJAX / REST call
Mar 02, 2016 06:03 AM|sw-ing|LINK
hi,
i'd like to know if ajax post request could be sent to call web method. besides, you could refer to this link to download and save files.
http://www.html5rocks.com/en/tutorials/file/xhr2/
Star
7970 Points
1586 Posts
Re: How do I build a pdf file/blob from byte[] via a AJAX / REST call
Apr 01, 2016 08:05 AM|Weibo Zhang|LINK
Hi EddieFlo,
According to your above description, I think you want to download a PDF file through the ajax and web service. The following link provides a demo about using Iframe to achieve it that you could have a look.
http://stackoverflow.com/questions/12601180/how-to-download-file-via-jquery-ajax-and-c-sharp
In link provides a demo about first build the contents of PDF through ajax calling web service during page loading and then use Server button to download the PDF, maybe you could have a look.
http://www.aspforums.net/Threads/482057/Export-GridView-populated-using-jQuery-AJAX-to-PDF-using-iTextSharp-in-ASPNet/
Besides, the following article shows a demo about using Ajax and a generic handler to do it and you could have a try if you need.
http://growingtech.blogspot.jp/2012/11/export-pdf-using-jquery-and-generic.html
Best Regards,
Albert Zhang