导航

文件下载实例

Posted on 2005-11-07 22:45  兰宝石.NET  阅读(247)  评论(0编辑  收藏  举报
 1using System;
 2using System.Collections;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Web;
 7using System.Web.SessionState;
 8using System.Web.UI;
 9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.IO;
12
13namespace UDS.SubModule.UnitiveDocument.Mail
14{
15    /// <summary>
16    /// Download 的摘要说明。
17    /// </summary>

18    public class Download : System.Web.UI.Page
19    {
20        private void Page_Load(object sender, System.EventArgs e)
21        {
22            string destFileName = Request.QueryString["destFileName"]!=null?Request.QueryString["destFileName"]:"";
23            destFileName = Server.MapPath(".")+destFileName;
24            destFileName = Server.UrlDecode(destFileName);
25            if(File.Exists(destFileName))
26            {
27                FileInfo fi = new FileInfo(destFileName);
28                Response.Clear();
29                Response.ClearHeaders();
30                Response.Buffer = false;
31                
32                Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(Path.GetFileName(destFileName),System.Text.Encoding.Default));
33                Response.AppendHeader("Content-Length",fi.Length.ToString());
34                Response.ContentType="application/octet-stream";
35                Response.WriteFile(destFileName);
36                Response.Flush();
37                Response.End();
38            }

39            else
40            {
41                Response.Write("<script langauge=javascript>alert('文件不存在!');history.go(-1);</script>");
42                Response.End();
43            }

44        }

45
46        Web Form Designer generated code
65    }

66}

67