.net 文件点击直接下载函数 记录

void down(string filepath)
        {
            //确定文件的物理路径
            string filePath = Server.MapPath(filepath);
            FileInfo Fi = new FileInfo(filePath);
            if (Fi.Exists)
            {
                FileStream fs = new FileStream(filePath, FileMode.Open);
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Fi.Name, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.Write("<script>alert('文件不存在!');</script>");
            }
        }
posted on 2011-04-07 15:45  锥子  阅读(349)  评论(0编辑  收藏  举报