andyhutch77 / MvcRazorToPdf

Create pdf documents within an asp .net mvc project by generating your views as normal but returning a PdfActionResult. This converts regular produced razor/html to pdf documents in the browser using the iTextXmlWorker.
125 stars 105 forks source link

How to download file using ajax call? #49

Open tvrajja opened 8 years ago

tvrajja commented 8 years ago

How to download file using ajax call? i want to return Json(new { success = "Valid" }, JsonRequestBehavior.AllowGet);

ie. how to convert the below line in actioncontroller.

return new PdfActionResult(pdfres, (writer, document) => { document.SetPageSize(new Rectangle(PageSize.A4)); document.NewPage(); }) { FileDownloadName = "ElanWasHere.pdf" };

tvrajja commented 8 years ago

Right now i am using window.location.href = url;. i need to do some other database updates after downloading how to do ?

FireSBurnsmuP commented 7 years ago

I can't pretend I know this plugin well enough to suggest a solution directly using it, but I'm pretty sure there isn't a supported method, as I'm pretty sure you're hitting a limitation of current browser tech.

My first thought on your particular situation is not to use an AJAX call - just go to the URL like you already are, but pass some information to the controller you're hitting in the URI params and have that controller do the additional processing when the parameters are present.

So just process the other database updates in the controller that generates the pdf (as mentioned).

It's possible you could also load the file in a hidden <iframe> and use that frame's onload event to run your other AJAX calls to update your database or whatever, but that seems a little extra-complicated to me.

[EDIT] sorry for necromancy, but maybe my suggestions will help someone else, since this person obviously solved their problem since then.