Post Back not occuring after pdf is downloaded

后端 未结 1 705
后悔当初
后悔当初 2021-01-27 08:50

I\'ve developed a pdf file using itextsharp.

Pdf generation is working fine. After the pdf is created it\'s being downloaded.

My problem is when the user clicks

相关标签:
1条回答
  • 2021-01-27 09:16

    Postback is occurring since the file is being created. Try the given solution. Your GetPDF(string quote_num) function is doing two tasks that you should break into two functions.

    1. Creating the pdf document.
    2. Downloading the pdf file after it is done.

    Now, After you have created the document, you should clear the controls and then send the file as response. Therefore do it as follows:

    Create pdf file.

    public void CreatePDF(string quote_num)
    {
        string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() +"-Quotation.pdf";
        Document disclaimer = new Document(PageSize.A4, 2, 2, 10, 10);
        PdfWriter writer = PdfWriter.GetInstance(disclaimer, new FileStream(url, FileMode.Create));
        writer.PageEvent = new myPDFpgHandler(quote_num);
        disclaimer.SetMargins(70, 10, 60, 80);    
        disclaimer.Open();
        GenerateQuotPDF getpdf = new GenerateQuotPDF();
        disclaimer = getpdf.GetPDFparams(disclaimer,quote_num, Session["empcd"].ToString(),txt_contactperson.Text,txt_contact1.Text,txt_company.Text,txt_address.Text,ddl_gene_desc.SelectedItem.ToString(),ddl_canopy.SelectedItem.ToString(),ddl_gene_type.SelectedItem.ToString(),txt_rentalamount.Text,txt_hours.Text,txt_variable.Text,ddl_terms.SelectedItem.ToString(),txt_remarks.Text,txt_technical.Text,ddl_sign1.SelectedValue,ddl_sign2.SelectedValue,txt_designation.Text,DateTime.Now);
        disclaimer.Close();
    }
    

    Reset the controls.

    ClearAllFields(); 
    

    Send the file as response.

    public void SendPDF(string url)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(url);
        if (file.Exists)
        {
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(url);
            Response.AddHeader("content-disposition", "attachment; filename=" + Session["empcd"].ToString() + "-Quotation.pdf");
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
    

    Note that I also added Response.End() to clear the buffer.

    0 讨论(0)
提交回复
热议问题