ASP.NET文件下载函数使用浅析
创始人
2024-04-23 07:20:11
0

ASP.NET文件下载函数使用是什么情况呢?在你的Page_Load中添加这样的代码:

  1. Page.Response.Clear();  
  2. bool success = ResponseFile(Page.Request, Page.Response, "目的文件名称", @"源文件路径", 1024000);  
  3.  if (!success)  
  4.      Response.Write("下载文件出错!");  
  5. Page.Response.End(); 

ASP.NET文件下载函数代码为:

  1. public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)  
  2.     {  
  3.         try 
  4.         {  
  5.             FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
  6.             BinaryReader br = new BinaryReader(myFile);  
  7.             try 
  8.             {  
  9.                 _Response.AddHeader("Accept-Ranges", "bytes");  
  10.                 _Response.Buffer = false;  
  11.                 long fileLength = myFile.Length;  
  12.                 long startBytes = 0;  
  13.        
  14.                 double pack = 10240; //10K bytes  
  15.                 //int sleep = 200;   //每秒5次   即5*10K bytes每秒  
  16.                 int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;  
  17.                 if (_Request.Headers["Range"] != null)  
  18.                 {  
  19.                     _Response.StatusCode = 206;  
  20.                     string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});  
  21.                     startBytes = Convert.ToInt64(range[1]);  
  22.                 }  
  23.                 _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());  
  24.                 if (startBytes != 0)  
  25.                 {  
  26.                     //Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));  
  27.                 }  
  28.                 _Response.AddHeader("Connection", "Keep-Alive");  
  29.                 _Response.ContentType = "application/octet-stream";  
  30.                 _Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );  
  31.                
  32.                 br.BaseStream.Seek(startBytes, SeekOrigin.Begin);  
  33.                 int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;  
  34.  
  35.                 for (int i = 0; i < maxCount; i++)  
  36.                 {  
  37.                     if (_Response.IsClientConnected)  
  38.                     {  
  39.                         _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));  
  40.                         Thread.Sleep(sleep);  
  41.                     }  
  42.                     else 
  43.                     {  
  44.                         i=maxCount;   
  45.                     }  
  46.                 }  
  47.             }  
  48.             catch 
  49.             {  
  50.              return false;  
  51.             }  
  52.             finally  
  53.             {  
  54.                 br.Close();  
  55.  
  56.                 myFile.Close();  
  57.             }  
  58.         }  
  59.         catch 
  60.         {  
  61.             return false;  
  62.         }  
  63.         return true;  
  64.     } 

这样就实现了文件下载时,不管是什么格式的文件,都能够弹出打开/保存窗口.

ASP.NET文件下载函数的基本情况就向你介绍到这里,希望对你了解ASP.NET文件下载函数有所帮助。

【编辑推荐】

  1. ASP.NET项目开发中健康监视浅析
  2. ASP.NET缓存概念及其应用浅析
  3. ASP.NET缓存分析和实践浅析
  4. ASP.NET数据库缓存浅析
  5. ASP.NET源码之自定义控件DateTimePicker

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...