【发布时间】:2013-08-08 20:53:35
【问题描述】:
我正在尝试使用代码隐藏 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();
}
}
这可行,但是当文件被下载并保存时,它的内容只是一个 HTML 页面。
我在这里做错了什么?
【问题讨论】:
标签: c# asp.net web-applications download