熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

ASP.NET文件下載函數使用淺析

2022-06-13   來源: ASP編程 

  ASPNET文件下載函數使用是什麼情況呢?在你的Page_Load中添加這樣的代碼:

  PageResponseClear();  bool success = ResponseFile(PageRequest PageResponse 目的文件名稱 @源文件路徑 );   if (!success)       ResponseWrite(下載文件出錯!);  PageResponseEnd();
ASPNET文件下載函數代碼為:

  public static bool ResponseFile(HttpRequest _RequestHttpResponse _Responsestring _fileNamestring _fullPath long _speed)      {          try         {              FileStream myFile = new FileStream(_fullPath FileModeOpen FileAccessRead FileShareReadWrite);              BinaryReader br = new BinaryReader(myFile);              try             {                  _ResponseAddHeader(AcceptRanges bytes);                  _ResponseBuffer = false;                  long fileLength = myFileLength;                  long startBytes = ;                         double pack = ; //K bytes                  //int sleep = ;   //每秒次   即*K bytes每秒                  int sleep = (int)MathFloor( * pack / _speed) + ;                  if (_RequestHeaders[Range] != null)                  {                      _ResponseStatusCode = ;                      string[] range = _RequestHeaders[Range]Split(new char[] {= });                      startBytes = ConvertToInt(range[]);                  }                  _ResponseAddHeader(ContentLength (fileLength startBytes)ToString());                  if (startBytes != )                  {                      //ResponseAddHeader(ContentRange stringFormat( bytes {}{}/{} startBytes fileLength fileLength));                  }                  _ResponseAddHeader(Connection KeepAlive);                  _ResponseContentType = application/octetstream;                  _ResponseAddHeader(ContentDispositionattachment;filename= + HttpUtilityUrlEncode(_fileNameSystemTextEncodingUTF) );                                 brBaseStreamSeek(startBytes SeekOriginBegin);                  int maxCount = (int) MathFloor((fileLength startBytes) / pack) + ;                   for (int i = ; i < maxCount; i++)                  {                      if (_ResponseIsClientConnected)                      {                          _ResponseBinaryWrite(brReadBytes(intParse(packToString())));                          ThreadSleep(sleep);                      }                      else                     {                          i=maxCount;                       }                  }              }              catch             {               return false;              }              finally              {                  brClose();                   myFileClose();              }          }          catch         {              return false;          }          return true;      }
這樣就實現了文件下載時不管是什麼格式的文件都能夠彈出打開/保存窗口

  ASPNET文件下載函數的基本情況就向你介紹到這裡希望對你了解ASPNET文件下載函數有所幫助


From:http://tw.wingwit.com/Article/program/ASP/201311/21650.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.