ASP.NET provides file download functions

xiaoxiao2021-03-06  14

/ / Output hard disk file, provide download support large files, renewal, speed limit, resource occupation

// Enter the parameter _Request: Page.Request object, _Response: page.response object, _filename: Download file name, _fullpath: with file name download path, _speed Allow the number of bytes per second

// Return to whether it is 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

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 / Oct-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.clear (); bool success = responsefile (page.Request, page.response, "filename", @ "c: /download.date", 1024000); if (! Success) response.write ("Download File Error! ");

Page.Response.end ();

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

New Post(0)