.net文件下载

文件下载:

  protected void downFile(string FileName)
    {
        try
        {
            if (FileName != "")
            {
                string path = Server.MapPath("file\\") + FileName;
                System.IO.FileInfo fi = new System.IO.FileInfo(path);
                if (fi.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                    Response.AddHeader("Content-Length", fi.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.Filter.Close();
                    Response.WriteFile(fi.FullName);
                    Response.End();
                }
            }
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('下载失败。')</script>");// myClass.PopupMessage("下载失败!");
        }

    } 

 

posted @ 2012-04-27 14:12  】Richard【  阅读(281)  评论(0编辑  收藏  举报