0

I have a data table created with jquery. I have a jquery code to get the id of the row I selected from this table. According to this id, I make transactions from the database and create a pdf file. But when I say "download" the pdf file I created, my code doesn't work.

My "PDF İndir" button code

<a class="btn btn-sm btn-info pull-right" onclick="PDFIndir('/Raporlama/PdfIndir/', this)" id="btnPdf"><i class="fa fa-file-pdf-o"></i>  PDF İndir</a>

My "PdfIndir jquery code

function PDFIndir(urlx,t)
{
   var id = $(t).parents(".ui-dialog").find("table").attr("id");
   var seciliid;
   $("#"+id+" tbody tr.selected").each(function(){  
       seciliid = ($(t).parents(".ui-dialog").find(".ui-dialog- 
       content").find("table").DataTable().row($(this)).data().id);
   });
   $.ajax({
       type:"GET",
       url:urlx,
       data:seciliid
   });

}

My ActionResult code

    public ActionResult PdfIndir()
    {
        var id = new Guid(Request.QueryString.ToString());
        MemoryStream str = new MemoryStream();

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
                sb.Append("<tr><td style='background-color:#ffffff; width: 100%;' colspan='2'><img src='C:/TfsProject/Atm/atm.server.ui/images/at4.png' /></td></tr>");
                sb.Append("<tr><td><b>ABONE NO: </b>");
                sb.Append(aboneNo);
                sb.Append("</td><td align='right'><b>TARIH: </b>");
                sb.Append(DateTime.Now);
                sb.Append("</td></tr>");
                sb.Append("<tr><td colspan='2'><b>ABONE ADI: </b>");
                sb.Append(ad);
                sb.Append("</td></tr>");
                sb.Append("</table>");
                sb.Append("<br />");

                StringReader sr = new StringReader(sb.ToString());

                HTMLWorker parse = new HTMLWorker(pdfDoc);

                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                parse.Parse(sr);
                pdfDoc.Close();


                this.Response.ContentType = "application/octet-stream";
                this.Response.AddHeader("content-disposition", "attachment;filename=" + dosyaAdi + ".pdf");
                this.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
                this.Response.Write(pdfDoc);
                this.Response.End();

                return View();
            }
        }
    }
aynurk
  • 21
  • 3
  • 1
    Avoid using AJAX to download a file, use standard `location.href` instead. Also you're not passing `seciliid` to the controller action, what kind of value contained inside that? – Tetsuya Yamamoto Oct 30 '18 at 07:38
  • I have to use Ajax. I can't take id from the table otherwise. The value of seciliid is a value of Guid – aynurk Oct 30 '18 at 07:44
  • I get the necessary data from the database using id and create the pdf file with that data. Is there another way to create a PDF file? – aynurk Oct 30 '18 at 07:52
  • Is that `seciliid` contains actual GUID when using JS? You can find out the reason why I suggested standard `window.location` or `location.href` over AJAX: https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax. – Tetsuya Yamamoto Oct 30 '18 at 07:55
  • "I have to use Ajax. I can't take id from the table otherwise"...that's not true, you could pre-populate a hyperlink with the ID when creating the HTML of the table. In fact that's a very normal way to do it. Tetsuya is right, it's impossible to download a file via AJAX - the downloaded data simply ends up in a JS variable in your browser code, rather than in a file on the user's disk. – ADyson Oct 30 '18 at 10:14

0 Answers0