从.aspx页面输出文件

ahm*_*md0 3 c# asp.net web-applications download

我正在尝试使用代码隐藏的C#代码从我的DownloadFile.aspx页面输出文件.我做以下事情:

protected void Page_Load(object sender, EventArgs e)
{
    string strFilePath = @"C:\Server\file";
    string strFileName = @"downloaded.txt";

    long uiFileSize = new FileInfo(strFilePath).Length;

    using (Stream file = File.OpenRead(strFilePath))
    {
        Response.ContentType = "application/octet-stream";

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

        Response.OutputStream.CopyTo(file);

        Response.End();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但是当文件被下载并保存时,其内容只是一个HTML页面.

我在这做错了什么?

Joe*_*nos 7

你正在向后复制流.应该:

file.CopyTo(Response.OutputStream);
Run Code Online (Sandbox Code Playgroud)