文件下载

 public void daochu(string path,string name)     {

        if (File.Exists(path))         {   

      FileInfo file = new FileInfo(path);       

      HttpContext.Current.Response.Clear();      

      HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(name, Encoding.UTF8));        

      HttpContext.Current.Response.AddHeader("Content-Lenght", file.Length.ToString());

      HttpContext.Current.Response.ContentType = "application/octet-stream";

       //以字符流的形式下载     

       FileStream fs = new FileStream(path, FileMode.Open);   

       byte[] bytes = new byte[(int)file.Length];       

       fs.Read(bytes, 0, bytes.Length);   

       fs.Close();

            //下载     

      HttpContext.Current.Response.BinaryWrite(bytes);  

      HttpContext.Current.Response.Flush();            

      HttpContext.Current.Response.End();        

   }

}

 

posted @ 2015-03-27 09:37  牡丹  阅读(74)  评论(0)    收藏  举报