.net 附件下载

      突然感觉现在爱上了博客园,在这里可以看到许多网友的分享,可以一起平复受伤的心,所以,我也要充分利用博客园,将自已之前零零散散记录在记事本中的总结,记录在这里。与大家一起学习,也方便自己使用。

      本节总结,几种下载文件的方法,也是从网上搜到的。

        //TransmitFile下载
        protected void Button1_Click(object sender, EventArgs e)
        {
            /*        
             * 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite   
             * 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。    
             * 代码如下:     
             *
             */
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlEncode("中国建设银行E路护航网银安全组件(大明).rar",Encoding.UTF8));
            string filename = Server.MapPath("中国建设银行E路护航网银安全组件(大明).rar");
            Response.TransmitFile(filename);
        }
        //流下载
        protected void Button2_Click(object sender, EventArgs e)
        {
            string fileName = "下载文件.rar";//客户端保存的文件名    
            string filePath = Server.MapPath(fileName);//转化为当前项目下的路径    
            FileInfo fileInfo = new FileInfo(filePath);     
            Response.Clear();   
            Response.ClearContent();    
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));   
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());    
            Response.AddHeader("Content-Transfer-Encoding", "binary");   
            Response.ContentType = "application/octet-stream";     
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");   
            Response.WriteFile(fileInfo.FullName);     
            Response.Flush();   
            Response.End();
       
        }
         /// <summary>
        /// 分块下载文件
        /// </summary>
        /// <param name="strPath">文件的相对路径</param>
        /// <param name="fileName">文件名称</param>
        protected void fileDown(string strPath, string fileName)
        {
            string filePath = Server.MapPath(strPath);//转化为当前项目下的路径     
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
            if (fileInfo.Exists == true)
            {  //文件存在
                const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 
                byte[] buffer = new byte[ChunkSize];
                Response.Clear();
                System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
                long dataLengthToRead = iStream.Length;//获取下载的文件总大小                

                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
                while (dataLengthToRead > 0 && Response.IsClientConnected)
                {
                    int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小   
                    Response.OutputStream.Write(buffer, 0, lengthRead);
                    Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }
                Response.Flush();
                Response.End();
                Response.Close();
            }
            else
            {              
                Response.Write("<script>window.opener=null;window.open('','_top');alert('文件不存在!');window.top.close();</script>");
               
            }
        }

 

public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )

{

  WebForm.Response.ClearHeaders();

  WebForm.Response.Clear();

  WebForm.Response.Expires = 0;

  WebForm.Response.Buffer = true;

  WebForm.Response.AddHeader("Accept-Language", "zh-tw");

  //文件名称

  WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'");

  WebForm.Response.ContentType = "Application/octet-stream";

  //文件内容

  WebForm.Response.Write(FileBody);

     WebForm.Response.End();

}

//上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:

public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )

{

  WebForm.Response.ClearHeaders();

  WebForm.Response.Clear();

  WebForm.Response.Expires = 0;

    WebForm.Response.Buffer = true;

  WebForm.Response.AddHeader("Accept-Language", "zh-tw");

  //文件名称

  WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );

  WebForm.Response.ContentType = "Application/octet-stream";

  //文件内容

  WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath));

  WebForm.Response.End();

}

 

posted @ 2013-03-22 09:32  dreamfly_cc  阅读(847)  评论(0编辑  收藏  举报