ASP.NET provides file download functions (support large files, renewal, speed limit, resource occupation)

xiaoxiao2021-03-06  41

Original 9CBS User Maling (***) () // Output Hard Disk file, provide download // input parameters _Request: page.request object, _RESPONSE: Page.Response object, _FileName: Download file name, _FullPath: with file name download path, _speed per second. // returns the number of bytes downloaded successful public static bool ResponseFile (HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed) {try {FileStream myFile = new FileStream (_fullPath, FileMode.Open , FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader (myFile); try {_Response.AddHeader ( "Accept-Ranges", "bytes"); _Response.Buffer = false; long fileLength = myFile.Length; long StartBytes = 0; int pack = 10240; // 10K bytes // int SLEEP = 200; // 5 times per second, 5 * 10K Bytes per second INT SLEP = (int) Math.Floor (1000 * pack / _speed) 1; if (_Request.Headers ["Range"]! = Null) {_RESPONSE.STATUSCODE = 206; String [] Range = _Request.Headers ["Range"]. Split (new char [] {'=', '- '}); StartBytes = Convert.Toint64 (Range [1]);} _response.addheader "Content-Length", (FileLength - StartBytes) .tostring ()); if (StartBytes! = 0) {_RESPONSE.ADDHEADER ("Content-Range", string.format ("bytes {0} - {1} 2} ", startBytes, fileLength-1, fileLength));} _Response.AddHeader (" Connection "," Keep-Alive "); _Response.ContentType =" application / octet-stream "; _Response.AddHeader (" Content-Disposition "," attachment; filename = " httputility.urlencode (_FileName, System.Text.Encoding.utf8)); br.basestream.seek (startbytes, seekorigin.begin); int maxcount =

(int) Math.Floor (FileLength - StartBytes / Pack) 1;

For (int i = 0; i

Page.Response.end ();

转载请注明原文地址:https://www.9cbs.com/read-82793.html

New Post(0)