1

I have pdfExport class where I generate a pdf. In this class I have a method

    public MemoryStream returnPDF()
    {
        using (MemoryStream stream = new MemoryStream())
        {
            pdfRenderer.PdfDocument.Save(stream, false);
            return stream;
        }

    }

My controller looks as follows

     [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult Contingency_Report(List<int> ids)
    {
        pdfExport pdf = new pdfExport(ids);

        MemoryStream stream = new MemoryStream();

        stream = pdf.returnPDF();
        return File(stream.ToArray(), "application/pdf", "contingency.pdf");

    }

As I can see in Chrome (F12 key is pressed), the response returns pdf (the Response tab), but the file is not available for download. Nothing happens till I see results in Response tab. How to make it available for download? I want the browser ask where to save file.

Liam
  • 27,717
  • 28
  • 128
  • 190
lazerbrain
  • 171
  • 1
  • 4
  • 17

1 Answers1

4

I solved my problem. If anyone needs help there is what I have done

View:

    $(function () {
    $("#btnCont").click(function () {
        var idsToSend = [];

        var grid = $("#grid_Edit").data("kendoGrid");
        var ds = grid.dataSource.view();

        for (var i = 0; i < ds.length; i++) {
            var row = grid.table.find("tr[data-uid='" + ds[i].uid + "']");
            var checkbox = $(row).find(".checkbox");

            if (checkbox.is(":checked")) {
                idsToSend.push(ds[i].ID);
            }
        }

        //      alert(idsToSend);

        //  $.post("/Contigency/Contingency_Report", { ids: idsToSend});

        var url = "";
        url = server;

        var getUrl = '@Url.Action("Download_Report", "Contigency")';

        $.ajax({
            url: url + "/Contigency/Contingency_Report",
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify({
                ids: idsToSend 
            }),
            success: function (d) {
                if (d.success) {
                          window.location = getUrl + "?fName="+ d.fName;
                              }
             },
             error: function () {

             }

        })
    })
}

 );

And post and get methods

            public ActionResult Contingency_Report(List<int> ids)
    {
        MemoryStream workStream = new MemoryStream();

        pdfExport pdf = new pdfExport(ids);

        workStream = pdf.returnPDF();

        workStream.Position = 0;

        var fName = string.Format("Contingency-{0}", DateTime.Now.ToString("s"));
        Session[fName] = workStream;

        return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet);
    }

    public ActionResult Download_Report(string fName)
    {
        var cd = new System.Net.Mime.ContentDisposition
        {
            FileName = fName+".pdf",
            Inline = false,
        };

        var stream = Session[fName] as MemoryStream;
        if (stream == null)
            return new EmptyResult();

        Session[fName] = null;

        Response.AppendHeader("Content-Disposition", cd.ToString());

        return File(stream, System.Net.Mime.MediaTypeNames.Application.Pdf);
    }

That's it!

lazerbrain
  • 171
  • 1
  • 4
  • 17