none
File Download Problem in Asp.net C# RRS feed

  • Question

  • Dear Team,

    I am trying to download a file from my Server.Mappath("/Test.txt"); file. I am getting a different error Please watch the error guide me what to do.

    Code : 

    proteced void lnkDownload_Click(object sender,EventArgs e)
    {
         string strFileName = lnkDownload.Text;
         string path = Server.MapPath("~/Attachments//" + strFileName);
         try
         {
         	if (File.Exists(path))
         	{
              byte[] bts = System.IO.File.ReadAllBytes(path);
              MemoryStream ms = new MemoryStream(bts);
              Response.AddHeader("Content-Disposition", "attachment;filename=\"" + strFileName + "\"");
              Response.TransmitFile(path);
              Response.End();
         	}	
         }
         catch(Exception ex)
         {
    	throw ex;
         }	
    
    }

    Error Details: 

    When code execution reaches to Response.End() there it is giving some unknown error

    

    exception details showing like above attached image. But final exception comming like 

    System.Threading.ThreadAbortException: Thread was being aborted.

       at System.Threading.Thread.AbortInternal()

       at System.Threading.Thread.Abort(Object stateInfo)

       at System.Web.HttpResponse.End()

    Please guide.

    Thanks in Advance

    Tvs Kumar

    Thursday, July 4, 2013 5:49 AM

Answers

  • It's working on edited code without any exception

    protected void lnkDownload_Click(object sender, EventArgs e)

        {

            string strFileName = "Test.txt";// lnkDownload.Text;

            string path = Server.MapPath("~/Attachments//" + strFileName);

            //try

            //{

                if (File.Exists(path))

                {

                    byte[] bts = System.IO.File.ReadAllBytes(path);

                    MemoryStream ms = new MemoryStream(bts);

                    Response.Clear();

                    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + strFileName + "\"");

                    Response.TransmitFile(path);

                    Response.End();

                }

            //}

            //catch (Exception ex)

            //{

            //    throw ex;

            //}    

        }

    Firstly, you should add the following code at the begining of  Page_Load event.

    Response.Clear();

    Secondly,you can't put 'Response.End();' into try catch block, it will cause the problem you mentioned.

    You can remove it and use it directly.

    Response.End(), aborts the current thread. if you call inside a try block,you will catch the thread abort. if you use a try block, you'll need to catch the abort and rethrow it.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    • Marked as answer by Tvs Kumar Thursday, July 4, 2013 6:22 AM
    Thursday, July 4, 2013 6:14 AM

All replies

  • It's working on edited code without any exception

    protected void lnkDownload_Click(object sender, EventArgs e)

        {

            string strFileName = "Test.txt";// lnkDownload.Text;

            string path = Server.MapPath("~/Attachments//" + strFileName);

            //try

            //{

                if (File.Exists(path))

                {

                    byte[] bts = System.IO.File.ReadAllBytes(path);

                    MemoryStream ms = new MemoryStream(bts);

                    Response.Clear();

                    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + strFileName + "\"");

                    Response.TransmitFile(path);

                    Response.End();

                }

            //}

            //catch (Exception ex)

            //{

            //    throw ex;

            //}    

        }

    Firstly, you should add the following code at the begining of  Page_Load event.

    Response.Clear();

    Secondly,you can't put 'Response.End();' into try catch block, it will cause the problem you mentioned.

    You can remove it and use it directly.

    Response.End(), aborts the current thread. if you call inside a try block,you will catch the thread abort. if you use a try block, you'll need to catch the abort and rethrow it.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    • Marked as answer by Tvs Kumar Thursday, July 4, 2013 6:22 AM
    Thursday, July 4, 2013 6:14 AM
  • Hi,

    Now I got the Issue. My user control is located inside the UpdatePanel. I just Commented Update panel everything is working fine. Can I do this with UpdatePanel.

    Thanks in Advance

    Tvs Kumar

    Thursday, July 4, 2013 6:22 AM
  • Yes it will work if you will define your control ID inside PostBackTrigger of Triggers. See below code for demo

    <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

            <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                <ContentTemplate>

                    <asp:LinkButton ID="lnkDownload" runat="server" OnClick="lnkDownload_Click">LinkButton</asp:LinkButton>

                </ContentTemplate>

                <Triggers>

                    <asp:PostBackTrigger ControlID="lnkDownload" />

                </Triggers>

            </asp:UpdatePanel>


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    Thursday, July 4, 2013 6:47 AM
  • Not Working...
    Thursday, August 29, 2013 8:28 AM
  • Try this example:

    public void TheDownload(string path)
    {
      System.IO.FileInfo toDownload = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path));

      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.AddHeader("Content-Disposition",
                 "attachment; filename=" + toDownload.Name);
      HttpContext.Current.Response.AddHeader("Content-Length",
                 toDownload.Length.ToString());
      HttpContext.Current.Response.ContentType = "application/octet-stream";
      HttpContext.Current.Response.WriteFile(patch);
      HttpContext.Current.Response.End();

    The implementation is done in the follows:

    TheDownload("@"c:\Temporal\Test.txt"");

    Source: http://www.systemdeveloper.info/2014/03/force-downloading-file-from-c.html

    Sunday, March 16, 2014 5:44 PM
  • Yes it will work if you will define your control ID inside PostBackTrigger of Triggers. See below code for demo

    <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

            <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                <ContentTemplate>

                    <asp:LinkButton ID="lnkDownload" runat="server" OnClick="lnkDownload_Click">LinkButton</asp:LinkButton>

                </ContentTemplate>

                <Triggers>

                    <asp:PostBackTrigger ControlID="lnkDownload" />

                </Triggers>

            </asp:UpdatePanel>


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    Thanks, run with sucess!
    Monday, September 11, 2017 6:04 PM