首页 新闻 会员 周边 捐助

c# 如何实现下载

0
悬赏园豆:100 [已解决问题] 解决于 2008-12-08 19:17

 

Code

 

请问接收到 GET /source.rar 时应该怎样处理,才能在实现下载。

王军的主页 王军 | 初学一级 | 园豆:110
提问于:2008-12-07 17:14
< >
分享
最佳答案
-1

一、

 //流方式下载 
  protected void Button4_Click(object sender, EventArgs e)
  {
  string fileName = "aaa.txt";//客户端保存的文件名 
  string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

  //以字符流的形式下载文件 
  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=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  Response.BinaryWrite(bytes);
  Response.Flush();
  Response.End();
  }

 

//----------------------------------------------------------

 

 

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();
}

二、
// 输出硬盘文件,提供下载  
  // 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数  
  // 返回是否成功  
  public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)  
  {  
  try  
  {  
  FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
  BinaryReader br = new BinaryReader(myFile);  
  try  
  {  
  _Response.AddHeader("Accept-Ranges", "bytes");  
  _Response.Buffer = false;  
  long fileLength = myFile.Length;  
  long startBytes = 0;  
   
  int pack = 10240; //10K bytes  
  //int sleep = 200; //每秒5次 即5*10K bytes每秒  
  int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;  
  if (_Request.Headers["Range"] != null)  
  {  
  _Response.StatusCode = 206;  
  string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});  
  startBytes = Convert.ToInt64(range[1]);  
  }  
  _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());  
  if (startBytes != 0)  
  {  
  _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));  
  }  
  _Response.AddHeader("Connection", "Keep-Alive");  
  _Response.ContentType = "application/octet-stream";  
  _Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );  
   
  br.BaseStream.Seek(startBytes, SeekOrigin.Begin);  
  int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;  
   
  for (int i = 0; i < maxCount; i++)  
  {  
  if (_Response.IsClientConnected)  
  {  
  _Response.BinaryWrite(br.ReadBytes(pack));  
  Thread.Sleep(sleep);  
  }  
  else  
  {  
  i=maxCount;  
  }  
  }  
  }  
  catch  
  {  
  return false;  
  }  
  finally  
  {  
  br.Close();  
  myFile.Close();  
  }  
  }  
  catch  
  {  
  return false;  
  }  
  return true;  
  }  
   
   
  调用例  
   
  Page.Response.Clear();  
   
  bool success = ResponseFile(Page.Request, Page.Response, "filename", @"C:\download.date", 1024000);  
   
  if(!success)  
  Response.Write("下载文件出错!");  
   
  Page.Response.End();

上不了岸的鱼 | 老鸟四级 |园豆:4613 | 2008-12-08 00:43
其他回答(1)
0
Astar | 园豆:40805 (高人七级) | 2008-12-07 18:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册