Don't think you are, know you are

博客园 首页 新随笔 管理


download.aspx 的代码

protected void Page_Load(object sender, EventArgs e)
    {
        string sGenName = Request.QueryString[0]; // 获取文件名

        System.IO.FileStream fs = null;
        fs = System.IO.File.Open(Server.MapPath(sGenName), System.IO.FileMode.Open);
        byte[] btFile = new byte[fs.Length];
        fs.Read(btFile, 0, Convert.ToInt32(fs.Length)); //将文件读到数组中
        fs.Close();
        Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName); //告诉浏览器我们发送给它的是一个附件类型的文件
        Response.ContentType = "application/octet-stream";  
        Response.BinaryWrite(btFile);
        Response.End();

    }

下载链接的页面

<a href="download.aspx?fileName=<%= getFileName %>" >  download </a>
<br />

 

打开的页面


<a href="<%= getFileName %>"> open </a>


getFileName 是一个属性,返回一个文件名。这里是个示例,实际应用中应该是从数据库中读取用Eval绑定,题外话就不多讲了。

posted on 2007-12-01 15:51  炭炭  阅读(992)  评论(0)    收藏  举报