Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,
Following is my code to download the pdf file using its path.File is stored in folder in my solution but it throw exception at Response.End(); "Thread was being aborted".
Can any one help me please..
Thanx in advance .


C#
string id;
     string idd;
     string FileName;

     public string Doc_File = "application/ms-word/notepad";
     public string Docx_File = "application/ms-word";
     public string MS_EXCEl_xls_File = "application/vnd.ms-excel";
     public string MS_EXCEl_xlsx_File = "application/vnd.ms-excel";
     public string JPG_Image_File = "image/jpg";
     public string PNG_Image_File = "image/png";
     public string GIF_Image_File = "image/gif";
     public string PDF_File = "application/pdf";
     protected void Page_Load(object sender, EventArgs e)
     {

         if (!IsPostBack)
         {
             BindGrid();
             btnBrows.Attributes.Add("onclick", "document.getElementById('" + UpResume.ClientID + "').click();");
         }
         id = string.Empty;
         try
         {
             id = Session["BID"].ToString();
         }
         catch (Exception)
         {

         }
         if (IsPostBack && UpResume.PostedFile != null && UpResume.HasFile)
         {
             try
             {
                 if ((UpResume.PostedFile != null))
                 {
                     FileName = Path.GetFileName(UpResume.PostedFile.FileName);
                     Session["BrochureName"] = FileName;
                     try
                     {
                         if (string.IsNullOrEmpty(FileName))
                            FileName= Session["FileName"].ToString() ;
                     }
                     catch (Exception)
                     {
                     }
                     //Save files to images folder
                     string strFileType = System.IO.Path.GetExtension(UpResume.FileName).ToString().ToLower();
                     try
                     {
                         if (string.IsNullOrEmpty(strFileType))
                             strFileType = Session["strFileType"].ToString();
                     }
                     catch (Exception)
                     {
                     }
                     string contenttype = string.Empty;
                     try
                     {
                         //string filetype = Session["strFileType"].ToString();
                         if (strFileType != null)
                         {
                             switch (strFileType)
                             {
                                 case ".doc":
                                     contenttype = "application/ms-word";
                                     break;
                                 case ".docx":
                                     contenttype = "application/ms-word";
                                     break;
                                 case ".pdf":
                                     contenttype = "application/pdf";
                                     break;
                             }
                             if (string.IsNullOrEmpty(contenttype))
                             {
                                 lblTitleInfo.Text = "ERROR...!!!!";
                                 lblMessage.Text = "File format not supported";
                                 lblMessage.ForeColor = System.Drawing.Color.Red;
                                 MsgPopUpInfo.Show();
                             }
                             else
                             {
                                 Session["extension"] = contenttype;
                             }
                         }
                     }
                     catch(Exception)
                     {

                     }

                     if (string.IsNullOrEmpty(id))
                     {
                         DataTable dtH = ObjMaster.FetchData(Transaction.getBrochuresidentity);
                         if (dtH.Rows.Count > 0)
                         {

                             idd = dtH.Rows[0]["id"].ToString();
                             string filePath = Server.MapPath(("../Brochure/" + idd + FileName));
                             UpResume.SaveAs(Server.MapPath("../Brochure/" + idd + FileName));

                             Session["PostedFile"] = UpResume.PostedFile;
                             Session["strFileType"] = strFileType;
                             // Session["FileName"] = lblUpPhoto.Text;
                             Session["FileName"] = "../Brochure/" + idd + FileName;

                         }
                     }
                     else
                     {
                         UpResume.SaveAs(Server.MapPath("../Brochure/" + id + FileName ));
                         Session["PostedFile"] = UpResume.PostedFile;
                         Session["strFileType"] = strFileType;
                         Session["FileName"] = "../Brochure/" + id + FileName;
                     }
                 }
             }
             catch (Exception)
             { }

         }
     }
public string getContentType(string filetype)
     {
         string contenttype = string.Empty;
         try
         {
             switch (filetype)
             {
                 case ".doc":
                     contenttype = Doc_File;
                     break;
                 case ".docx":
                     contenttype = Docx_File;
                     break;
                 case ".xls":
                     contenttype = MS_EXCEl_xls_File;
                     break;
                 case ".xlsx":
                     contenttype = MS_EXCEl_xlsx_File;
                     break;
                 case ".jpg":
                     contenttype = JPG_Image_File;
                     break;
                 case ".png":
                     contenttype = PNG_Image_File;
                     break;
                 case ".gif":
                     contenttype = GIF_Image_File;
                     break;
                 case ".pdf":
                     contenttype = PDF_File;
                     break;
             }
         }
         catch (Exception)
         {
         }
         return contenttype;
     }


     protected void btnSubmit_Click(object sender, EventArgs e)
     {
         try
         {
             if (btnSubmit.Text.ToLower().Equals("submit"))
             {
                 Getdata();

             }

             else if (btnSubmit.Text.ToLower().Equals("delete"))
             {
                 VarBrochures.BrochureID = Convert.ToInt64(Session["BID"].ToString());
                 ObjBrochures.Delete(VarBrochures.BrochureID);

              }
         }
         catch (Exception)
         {
         }
     }

      protected void lbtnBrochure_Click(object sender, EventArgs e)
     {
         try
         {
             string id = Session["BID"].ToString();
             DataTable dtdata = ObjMaster.FetchData(Transaction.ParticularBrochuresByID, null, null, null, null, Convert.ToInt32(id));
             if (dtdata.Rows.Count>0)
             {
                 string ext = Path.GetExtension(dtdata.Rows[0]["Content"].ToString());
                 string nm = dtdata.Rows[0]["Content"].ToString();
                 //lbtnBrochure.PostBackUrl = "../Brochure/" + nm;
                 Response.Clear();
                 Response.ContentType = "Application/pdf";
                 Response.AddHeader("Content-Disposition","attachment;filename="+nm);


                 string FilePath = Server.MapPath("../Brochure/" + nm);
                 Response.TransmitFile(FilePath);
                Response.Flush();

                Response.End();
                 //HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
                 //HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
                // HttpContext.Current.ApplicationInstance.CompleteRequest();
                 ClearAllData();
             }

         }
         catch (Exception ex)
         {

         }
         finally
         {

         }
     }
 }/pre>
Posted
Updated 31-Jan-16 20:33pm
v6

Replace

C#
Response.End();


with

C#
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
 
Share this answer
 
Comments
asplover1 1-Feb-16 0:49am    
Thank you for reply
asplover1 1-Feb-16 0:53am    
It neither throw exception nor download the file
Ebenezar John Paul 1-Feb-16 1:14am    
It works for me. Try to rebuild the project, take away all the breakpoints, do a clean run and check the downloads. The suggested solution works best in all cases of Thread being aborted exception caused by Response.End().
Member 14789376 7-Apr-20 4:30am    
Hi, did you find a solution? It neither throw exception nor download the file for my app too. If you found a solution can you share please?
Try to use ApplicationInstance.CompleteRequest instead of Response.End(). See below code:
C#
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + File1.pdf);
Response.TransmitFile(Server.MapPath("../Brochure/" + File1.pdf));
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

Reason: ApplicationInstance.CompleteRequest causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event. But Response.End() sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event. It also raise a ThreadAbortException exception.

HttpResponse.End Method (System.Web)[^]
 
Share this answer
 
v2
Comments
asplover1 1-Feb-16 1:10am    
Thanx for reply.
But it doesn't work. Now it doesn't throw exception but it doesn't show save as pop up for download
[no name] 1-Feb-16 1:19am    
Update your complete code by using "Improve question".
asplover1 1-Feb-16 2:10am    
I update my code please review it
[no name] 1-Feb-16 2:14am    
Where is the code for download file.

Secondly do not post whole code in your question. Post only relevant code.
asplover1 1-Feb-16 2:35am    
The code for download file is in lbtnBrochure_Click

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900