When i click a button then bootstrap popup comes where a button exist for download data as pdf. when button clicked on bootstrap popup then below code fire and call server side action.
Server side action execute properly but no pdf download dialog appear but when i invoke the same action putting in browser url then pdf download at client side.
see my server side action code
[Route("DownloadPDF")]
[HttpGet]
public void DownloadPDF()
{
//bool IsPdfGenerated = false;
List<Student> studentsVM = new List<Student>
{
new Student {ID=1,FirstName="Joy", LastName="Roy", FavouriteGames="Hocky"},
new Student {ID=2,FirstName="Raja", LastName="Basu", FavouriteGames="Cricket"},
new Student {ID=3,FirstName="Ajay", LastName="saha",FavouriteGames="Foot Ball"},
new Student {ID=4,FirstName="Debu", LastName="Saha", FavouriteGames="Tennis"},
new Student {ID=5,FirstName="Sanjeeb", LastName="Das", FavouriteGames="Hocky"},
};
var viewToString = StringUtilities.RenderViewToString(ControllerContext, "~/Views/Shared/_Report.cshtml", studentsVM, true);
string filepath = HttpContext.Server.MapPath("~/PDFArchives/") + "mypdf.pdf";
MemoryStream workStream = new MemoryStream();
StringReader sr = new StringReader(viewToString);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 30f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
//writer.CloseStream = false;
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
System.Web.HttpContext.Current.Response.ContentType = "pdf/application";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;" +
"filename=sample.pdf");
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Web.HttpContext.Current.Response.Write(pdfDoc);
System.Web.HttpContext.Current.Response.End();
}
what is wrong in my approach ?
pdf download box not coming because i am using bootstrap popup ? looking for guide line. thaks
In the ajax success function, the parameter named data, should be the pdf file. If you convert to a blob, assign as a dataurl to an anchor, then click the anchor, the browser will save it. But in you case it much easier to assign the url to a hidden anchor,
then click with jQuery.
Member
39 Points
73 Posts
ASP.Net MVC ItextSharp: PDF file not downloading when calling action by jquery
Sep 10, 2018 05:57 PM|dev_dona|LINK
When i click a button then bootstrap popup comes where a button exist for download data as pdf. when button clicked on bootstrap popup then below code fire and call server side action.
Server side action execute properly but no pdf download dialog appear but when i invoke the same action putting in browser url then pdf download at client side.
see my server side action code
what is wrong in my approach ?
pdf download box not coming because i am using bootstrap popup ? looking for guide line. thaks
All-Star
50854 Points
12097 Posts
Re: ASP.Net MVC ItextSharp: PDF file not downloading when calling action by jquery
Sep 10, 2018 07:16 PM|bruce (sqlwork.com)|LINK