KendoStarter
KendoStarter

Reputation: 139

ASP.Net MVC ItextSharp: PDF file not downloading when calling action by jquery

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.

$(".bol_save_as_pdf").click(function () {
    $.ajax({
    type: "GET",
    url: '@Url.Action("GeneratePDF", "TestModal")',
    success: function (data) {
        alert(data.Msg);
        },
        error: function (xhr, status, error) {
        //$("#dataDiv").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
        }
    });
    return false;
});

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="Das",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

Upvotes: 0

Views: 955

Answers (2)

Shaun
Shaun

Reputation: 16

Remove the ajax and replace your button with - @Url.Action("DownloadPDF", "Home")

Upvotes: 0

KendoStarter
KendoStarter

Reputation: 139

This issue is fixed now. the moment i change the js code it started working. just i need to use window.location = '@Url.Action("DownloadPDF", "TestModal")';

$("#btndownload").click(function () {
    window.location = '@Url.Action("DownloadPDF", "TestModal")';
    return false;
});

Upvotes: 1

Related Questions