asp.net 点击girdView中列 下载文件--源码,调试成功

在gridview中添加一个linkButton,为其添加CommandArgument参数,并设置CommandName 为download

在GridView1_RowCommand事件中添加如下代码

if (e.CommandName == "download")
        {
            String fileName = e.CommandArgument.ToString();
            if (fileName != "")
            {
                //获得文件的存储路径               
                string path = Server.MapPath(fileName);
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.Filter.Close();
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                {
                    Response.Write("<script>alert('文件不存在');</script>");
                }
            }

posted @ 2009-03-06 13:01  perfectMan  阅读(655)  评论(0)    收藏  举报