C# 通过ASP.Net网站下载文件

C# 通过ASP.Net网站下载文件,c#,asp.net,download,C#,Asp.net,Download,我正在向我的网站用户推送一个PEM文件以供下载。代码如下: try { FileStream sourceFile = null; Response.ContentType = "application/text"; Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(RequestFilePath)); sourceFile = new Fil

我正在向我的网站用户推送一个PEM文件以供下载。代码如下:

try
{
    FileStream sourceFile = null;

    Response.ContentType = "application/text";
    Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(RequestFilePath));
    sourceFile = new FileStream(RequestFilePath, FileMode.Open);
    long FileSize = sourceFile.Length;
    byte[] getContent = new byte[(int)FileSize];
    sourceFile.Read(getContent, 0, (int)sourceFile.Length);
    sourceFile.Close();
    Response.BinaryWrite(getContent);
}
catch (Exception exp)
{
    throw new Exception("File save error! Message:<br />" + exp.Message, exp);
}
试试看
{
FileStream sourceFile=null;
Response.ContentType=“应用程序/文本”;
AddHeader(“内容处置”、“附件;文件名=“+Path.GetFileName(RequestFilePath));
sourceFile=newfilestream(RequestFilePath,FileMode.Open);
long FileSize=sourceFile.Length;
byte[]getContent=新字节[(int)FileSize];
读取(getContent,0,(int)sourceFile.Length);
sourceFile.Close();
Response.BinaryWrite(getContent);
}
捕获(异常扩展)
{
抛出新异常(“文件保存错误!消息:
”+exp.Message,exp); }
问题是,下载的文件中包含了应该存在的内容+整个网页的HTML的副本

这里发生了什么事?

放置以下内容

Response.Clear();
以前

Response.ContentType = "application/text";
更新

正如@Amiram在他的评论中所说的(无论如何,我都要补充我自己)

Response.BinaryWrite(getContent);
加上

Response.End();
放置以下内容

Response.Clear();
以前

Response.ContentType = "application/text";
更新

正如@Amiram在他的评论中所说的(无论如何,我都要补充我自己)

Response.BinaryWrite(getContent);
加上

Response.End();
添加此行:

Response.ClearContent();
Response.ContentType = "application/text";
...
添加此行:

Response.ClearContent();
Response.ContentType = "application/text";
...
我同意。
即添加
Response.ClearContent()
响应之前。内容类型…

但根据


还是整页都写下来了

在末尾添加
Response.End()

但它抛出
System.Threading.ThreadAbortException

因此,我建议您添加额外的
catch
以捕获
System.Threading.ThreadAbortException
,并且不要将错误消息放入
响应中。写入
,否则它也将添加到您的文本文件中:

try
{
    FileStream sourceFile = null;
    Response.ClearContent(); // <<<---- Add this before `ContentType`.
    Response.ContentType = "application/text";
    .
    .
    Response.BinaryWrite(getContent);
    Response.End(); // <<<---- Add this at the end.
}
catch (System.Threading.ThreadAbortException) //<<-- Add this catch.
{
    //Don't add anything here.
    //because if you write here in Response.Write,
    //that text also will be added to your text file.
}
catch (Exception exp)
{
    throw new Exception("File save error! Message:<br />" + exp.Message, exp);
}
试试看
{
FileStream sourceFile=null;
Response.ClearContent();//我同意。
这就是在
Response.ContentType…

但根据


还是整页都写下来了

在末尾添加
Response.End()

但它抛出
System.Threading.ThreadAbortException

因此,我建议您添加额外的
catch
以捕获
System.Threading.ThreadAbortException
,并且不要将错误消息放入
响应中。写入
,否则它也将添加到您的文本文件中:

try
{
    FileStream sourceFile = null;
    Response.ClearContent(); // <<<---- Add this before `ContentType`.
    Response.ContentType = "application/text";
    .
    .
    Response.BinaryWrite(getContent);
    Response.End(); // <<<---- Add this at the end.
}
catch (System.Threading.ThreadAbortException) //<<-- Add this catch.
{
    //Don't add anything here.
    //because if you write here in Response.Write,
    //that text also will be added to your text file.
}
catch (Exception exp)
{
    throw new Exception("File save error! Message:<br />" + exp.Message, exp);
}
试试看
{
FileStream sourceFile=null;


Response.ClearContent();//代码在哪里执行?在HTTP处理程序中,在按钮单击中?在设置响应之前,首先清除它。@Hassanglzar-+1用于您的代码。这对我帮助很大。:)代码在哪里执行?在HTTP处理程序中,在按钮单击中?在设置响应之前,首先清除它。@Hassanglzar-+1用于您的代码。这对我帮助很大。:)仍然是全部“e页面被写入。@哈桑,你的意思是整个页面(以及你正在下载的文件的内容)都被删除了吗正在合并到正在下载的文件中?是的,它们是。首先是我想要的实际数据,然后是整个页面的呈现HTML。现在已修复。仍然会写入整个页面。@Hassan,你的意思是整个页面(以及您正在下载的文件的内容)都是正在合并到正在下载的文件中?是的,它们是。首先是我想要的实际数据,然后是整个页面的呈现HTML。现在已修复。仍然会写入整个页面。在结尾添加Response.End()以修复它。现在介于
Response.ClearContent()
Response.ContentType=“application/text”之间
我注意到在
Response.End()'之后生成了一个异常。但是该异常未定义。它的内容为:
ThreadAbortException`。有什么想法吗?整个页面仍在编写中。在修复它的末尾添加Response.End()。现在介于
Response.ClearContent()
Response.ContentType=“application/text”之间
我注意到在
Response.End()之后生成了一个异常。但是该异常未定义。它的内容是:
ThreadAbortException`。有什么想法吗?