0

I basically want a button that downloads files. I have the following code:

protected void downloadButton_Click(object sender, ImageClickEventArgs e)
{
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "filename=" + "abcd.txt");
        Response.TransmitFile(Server.MapPath("~/Uploads/Group/") + "abcd.txt");
        Response.End();

}

But nothing happens when I click it, and I get the following text in the output:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
The thread '<No Name>' (0x19d0) has exited with code 0 (0x0).
The thread '<No Name>' (0x1efc) has exited with code 0 (0x0).

I have the file "abcd.txt" in "Uploads/Group/" and I want to download it. What am i doing wrong?

balauru
  • 55
  • 1
  • 10

1 Answers1

0

When you call Response.End(), the thread aborts. This is normal and expected behavior.

When you send a text file, use the content type "text/plain".

If you want to download something , make sure you set it to download as an attachment. Here's some sample code from ASP.Net Download file to client browser:

 Response.Clear();
 Response.ClearHeaders();
 Response.ClearContent();
 Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
 Response.AddHeader("Content-Length", file.Length.ToString());
 Response.ContentType = "text/plain";
 Response.Flush();
 Response.TransmitFile(file.FullName);
 Response.End();
Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121