我基本上想要一个下载文件的按钮。我有以下代码:
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();
}
但是当我点击它时没有任何反应,我在输出中得到以下文字:
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).
我有文件&#34; abcd.txt&#34; in&#34;上传/群组/&#34;我想下载它。我做错了什么?
答案 0 :(得分:0)
当您致电Response.End()
时,线程将中止。这是正常和预期的行为。
发送文本文件时,请使用内容类型“text / plain”。
如果要下载某些内容,请确保将其设置为附件下载。以下是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();