c#读取txt文件的问题

SerapSignore 2009-12-18 11:34:10
我在Gridview有个<asp:HyperLink ID="HyperLink1" runat="server" Text="詳細" NavigateUrl='<%# Eval("DETAIL_FILE") %>'/>
这个连接到另一个页面 。。
下面是另一个页面的后台代码 。前台为建立aspx生成的代码 。没改过。。
protected override void PageLoad(object sender, EventArgs e)//初始化方法
{
string sfilePath = Request["path"];
string sfilename = System.IO.Path.GetFileName(sfilePath);
Download(Response, sfilePath, sfilename);
}
public void Download(HttpResponse Response, string downloadFile, string downloadFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(downloadFileName));
Response.Charset = "Shift_JIS";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift-jis");
Response.CacheControl = "Private";

byte[] buff = new byte[1024];
using (FileStream fs = File.OpenRead(downloadFile))
{
int len;
while ((len = fs.Read(buff, 0, buff.Length)) > 0)
{
if (len != buff.Length)
{
byte[] buff2 = new byte[len];
Array.Copy(buff, buff2, len);
Response.BinaryWrite(buff2);
}
else
{
Response.BinaryWrite(buff);
}
}
}
HttpContext.Current.ApplicationInstance.CompleteRequest();
}

现在能读到这个文件 。
我的问题是:
我的文件为TXT格式的 。。 但是读取到的txt文件会吧这个下载页面的ASPX 信息也包含进去。。
如果我去读取xls文件就不会有下载页面的信息。
怎么让当读出的是txt文件的时候不要包含aspx页面的数据

...全文
58 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
danjiewu 2009-12-18
  • 打赏
  • 举报
回复
既然不是显示页面那直接重写ProcessRequest方法好了。
其实不用继承Page,直接实现接口IHttpHandler就可以了


public override void ProcessRequest(HttpContext context)
{
string sfilePath = context.Request["path"];
string sfilename = System.IO.Path.GetFileName(sfilePath);
Download(context.Response, sfilePath, sfilename);
}
SerapSignore 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ojlovecd 的回复:]
试试:
protected override void PageLoad(object sender, EventArgs e)//初始化方法
        {
            string sfilePath = Request["path"];
            string sfilename = System.IO.Path.GetFileName(sfilePath);
            Download(Response, sfilePath, sfilename);
           Response.End();        }

[/Quote]
强大 。OK 了~~ 非常感谢~~
我姓区不姓区 2009-12-18
  • 打赏
  • 举报
回复
试试:
protected override void PageLoad(object sender, EventArgs e)//初始化方法
{
string sfilePath = Request["path"];
string sfilename = System.IO.Path.GetFileName(sfilePath);
Download(Response, sfilePath, sfilename);
Response.End(); }
SerapSignore 2009-12-18
  • 打赏
  • 举报
回复
大家能够给另一个思路让我当点击这个link时候下载文件也行。。

110,550

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧