请问怎样用C#写一个下载功能?

解决方案 »

  1.   

    FileInfo Fi = new FileInfo(filePath);
    if (Fi.Exists)
    {
      FileStream fs = new FileStream(filePath, FileMode.Open);
      byte[] bytes = new byte[(int)fs.Length];
      fs.Read(bytes, 0, bytes.Length);
      fs.Close();
      Response.ContentType = "application/octet-stream";
      Response.AddHeader("Content-Disposition", "attachment; filename=1.excel");
      Response.BinaryWrite(bytes);
      Response.Flush();
      Response.End();
    }
      

  2.   

    //提供下载的文件,不编码的话文件名会乱码   
        private string fileName = HttpContext.Current.Server.UrlEncode("陈百强-一生何求.mp3");
        private string filePath = HttpContext.Current.Server.MapPath("~\\Music\\陈百强-一生何求.mp3");    
        //使用TransmifFile下载文件
        protected void Button1_Click(object sender, EventArgs e)
        {
            FileInfo info = new FileInfo(filePath);
            long fileSize = info.Length;
            Response.Clear();
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            //不指明Content-Length用Flush的话不会显示下载进度   
            Response.AddHeader("Content-Length", fileSize.ToString());
            Response.TransmitFile(filePath, 0, fileSize);
            Response.Flush();
            Response.Close();
        }
      

  3.   

    以上两种方法如果被迅雷抓到,下载的会是当前的HTML页面不知各位大侠有解决办法没?