ASP.NET实现数据流下载代码

System.IO.FileStream r = new System.IO.FileStream(FileName, System.IO.FileMode.Open);    //打开要下载的文件  
  //设置基本信息  
   Response.Buffer = false;  
   Response.AddHeader("Connection", "Keep-Alive");  
   Response.ContentType = "application/octet-stream";  
   Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(FileName));  
   Response.AddHeader("Content-Length", r.Length.ToString());  
   while (true)  
   {  
   //开辟缓冲区空间  
   byte[] buffer = new byte[1024];  
   //读取文件的数据  
   int leng = r.Read(buffer, 0, 1024);  
   if (leng == 0)//到文件尾,结束  
   break;  
   if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入  
   Response.BinaryWrite(buffer);  
   else  
   {  
   //读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块  
   byte[] b = new byte[leng];  
   for (int i = 0; i < leng; i++)  
   b[i] = buffer[i];  
   Response.BinaryWrite(b);  
   }  
   }  
   r.Close();//关闭下载文件  
   Response.End();//结束文件下载,该方法有下载提示框,服务器端可以知道何时下载完毕。



文章来自: 网络
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:

评论: 18 | 引用: 0 | 查看次数: 10347
回复回复Chase[2017-09-02 05:29 AM | del]
You've the most impressive internet websites.
回复回复Latisha[2017-08-16 07:12 PM | del]
Thanks meant for delivering this kind of wonderful subject material.
回复回复Mirta[2017-08-14 00:12 AM | del]
I enjoy this website - its so usefull and helpfull.
回复回复Alysa[2017-08-13 02:50 AM | del]
Extremely individual pleasant website. Huge details offered on few gos to.
回复回复Lan[2017-08-04 04:29 PM | del]
Fizemos um mapeamento das melhores oportunidades de negócios na Web no próximo ano.
Jonathan taioba pega no braço do aluno e ensina passo a passo.
回复回复Will[2017-08-04 09:21 AM | del]
Há muitas pessoas que vivem de escrever artigos, resenhas para blogs e internet sites.
Há muitas possibilidades e esse mercado está sempre aquecido.
回复回复Gena[2017-07-26 12:49 AM | del]
Your knowledge is very significant.
回复回复Jestine[2017-07-20 00:10 AM | del]
Sustain the awesome work !! Lovin' it!
回复回复Forrest[2017-07-19 04:34 AM | del]
Thanks for sharing this superb internet site.
回复回复Floy[2017-07-13 11:32 AM | del]
Astonishingly user friendly website. Enormous details readily available on couple of gos to.
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启