附件的下载

protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            string destFileName = Request.QueryString["destFileName"] != null ? Request.QueryString["destFileName"] : "";
            destFileName = Server.MapPath("../") + destFileName;
            destFileName = Server.UrlDecode(destFileName);
            if (File.Exists(destFileName))
            {
                FileInfo fi = new FileInfo(destFileName);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;

                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(destFileName), System.Text.Encoding.Default));
                Response.AppendHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(destFileName);
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.Write("<script langauge=javascript>alert('文件不存在!');history.go(-1);</script>");
                Response.End();
            }
posted @ 2011-06-28 10:30  xfyn  阅读(166)  评论(0编辑  收藏  举报