自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET文件下載函數(shù)使用淺析

開發(fā) 后端
ASP.NET文件下載函數(shù)的使用使得我們的程序操作有了明顯的提升,那么本文就向你介紹ASP.NET文件下載函數(shù)的相關(guān)信息。

ASP.NET文件下載函數(shù)使用是什么情況呢?在你的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文件下載函數(shù)代碼為:

  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.     } 

這樣就實(shí)現(xiàn)了文件下載時,不管是什么格式的文件,都能夠彈出打開/保存窗口.

ASP.NET文件下載函數(shù)的基本情況就向你介紹到這里,希望對你了解ASP.NET文件下載函數(shù)有所幫助。

【編輯推薦】

  1. ASP.NET項(xiàng)目開發(fā)中健康監(jiān)視淺析
  2. ASP.NET緩存概念及其應(yīng)用淺析
  3. ASP.NET緩存分析和實(shí)踐淺析
  4. ASP.NET數(shù)據(jù)庫緩存淺析
  5. ASP.NET源碼之自定義控件DateTimePicker
責(zé)任編輯:仲衡 來源: cnblogs
相關(guān)推薦

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優(yōu)點(diǎn)

2009-07-20 16:09:39

2009-07-21 10:05:10

ASP.NET配置文件

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2009-08-04 15:20:59

ASP.NET數(shù)據(jù)驗(yàn)證數(shù)據(jù)驗(yàn)證控件

2009-08-07 17:59:35

控件設(shè)計(jì)器

2009-07-29 15:55:48

ASP.NET Req

2009-08-03 13:38:18

ASP.NET編程模型

2009-08-04 17:16:16

ASP.NET代碼優(yōu)化

2009-08-05 16:50:09

ASP.NET For

2009-07-24 18:02:46

ASP.NET編程

2009-07-27 15:34:11

MembershipASP.NET

2009-07-27 17:25:53

ASP.NET驗(yàn)證控件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號