ASP.NET文件下载函数使用浅析

开发 后端
ASP.NET文件下载函数的使用使得我们的程序操作有了明显的提升,那么本文就向你介绍ASP.NET文件下载函数的相关信息。

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
责任编辑:仲衡 来源: cnblogs
相关推荐

2009-08-03 10:07:20

ASP.NET Ses

2009-08-10 14:55:43

ASP.NET htt

2009-07-29 14:12:45

ASP.NET tra

2009-07-29 13:42:25

ASP.NET注释

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET优点

2009-07-21 10:05:10

ASP.NET配置文件

2009-07-20 16:09:39

2009-08-07 17:59:35

控件设计器

2009-08-04 15:20:59

ASP.NET数据验证数据验证控件

2009-07-29 15:55:48

ASP.NET Req

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-08-03 13:38:18

ASP.NET编程模型

2009-07-24 18:02:46

ASP.NET编程

2009-08-05 16:50:09

ASP.NET For

2009-08-04 17:16:16

ASP.NET代码优化

2009-07-27 17:25:53

ASP.NET验证控件

2009-08-05 16:17:29

ASP.NET For
点赞
收藏

51CTO技术栈公众号