Copy Code code as follows:
public static bool DownloadFile (HttpContext HttpContext, String FilePath, long speed)
{
BOOL ret = true;
Try
{
#region--Verify: HttpMethod, the requested file exists
Switch (HttpContext.Request.HttpMethod.ToUpper ())
{//currently supports only get and head methods
Case "GET":
Case "Head":
Break
Default
HttpContext.Response.StatusCode = 501;
return false;
}
if (! File.exists (FilePath))
{
HttpContext.Response.StatusCode = 404;
return false;
}
#endregion
#region Define Local variables
Long startbytes = 0;
int packsize = 1024 * 10; Block read, 10K bytes per block
String fileName = Path.getfilename (FilePath);
FileStream myFile = new FileStream (FilePath, FileMode.Open, FileAccess.Read, fileshare.readwrite);
BinaryReader br = new BinaryReader (myFile);
Long filelength = myfile.length;
int sleep = (int) math.ceiling (1000.0 * packsize/speed);//MS: Time interval for reading next block of data
String lastupdatetiemstr = FILE.GETLASTWRITETIMEUTC (FilePath). ToString ("R");
String eTag = Httputility.urlencode (FileName, Encoding.UTF8) + lastupdatetiemstr;//Extract request headers for easy recovery of downloads;
#endregion
#region--Verifies that the file is too large, is a continuation, and is repaired after the last date requested
if (Myfile.length > Int32.MaxValue)
The {//-------file is too large-------
HttpContext.Response.StatusCode = 413;//Request entity too large
return false;
}
if (httpcontext.request.headers["If-range"]!= null)//corresponding response header etag: filename + file Last modified time
{
----------has been modified since the last date requested--------------
if (httpcontext.request.headers["If-range"]. Replace ("\", "")!= ETag)
{//File modified
HttpContext.Response.StatusCode = 412;//preprocessing failed
return false;
}
}
#endregion
Try
{
#region-------Add an important response header, resolve the request header, and correlate validation-------------------
HttpContext.Response.Clear ();
HttpContext.Response.Buffer = false;
HttpContext.Response.AddHeader ("Content-md5", Getmd5hash (MyFile));//For validating files
HttpContext.Response.AddHeader ("accept-ranges", "bytes");/important: Renewal must
HttpContext.Response.AppendHeader ("ETag", "\" "+ ETag +" "");/important: Renewal must
HttpContext.Response.AppendHeader ("last-modified", LASTUPDATETIEMSTR)//write the last modified date to the response
HttpContext.Response.ContentType = "Application/octet-stream";//mime type: matches any file type
HttpContext.Response.AddHeader ("Content-disposition", "attachment;filename=" + httputility.urlencode (filename, Encoding.UTF8). Replace ("+", "%20"));
HttpContext.Response.AddHeader ("Content-length", (filelength-startbytes). ToString ());
HttpContext.Response.AddHeader ("Connection", "keep-alive");
httpContext.Response.ContentEncoding = Encoding.UTF8;
if (httpcontext.request.headers["Range"]!= null)
{//------if it is a renewal request, obtain the starting position of the continuation, that is, the number of bytes already downloaded to the client------
HttpContext.Response.StatusCode = 206;//Important: The continuation must indicate a local range response. Initial download defaults to 200
string[] Range = httpcontext.request.headers["range"]. Split (new char[] {' = ', '-'});/"bytes=1474560-"
Startbytes = Convert.toint64 (range[1])//number of bytes already downloaded, that is, the start of this download
if (Startbytes < 0 | | | startbytes >= filelength)
{//Invalid start position
return false;
}
}
if (startbytes > 0)
{//------if it is a renewal request, tell the client the start byte number, the total length, so that the client appends the data to the startbytes position----------
HttpContext.Response.AddHeader ("Content-range", String. Format ("bytes {0}-{1}/{2}", Startbytes, FileLength-1, filelength));
}
#endregion
#region-------Send a block of data to the client-------------------
Br. Basestream.seek (Startbytes, Seekorigin.begin);
int maxcount = (int) math.ceiling ((filelength-startbytes + 0.0)/packsize)//block download, number of blocks remaining to be divided
for (int i = 0; i < maxcount && httpContext.Response.IsClientConnected; i++)
{//client disconnected, paused
HttpContext.Response.BinaryWrite (Br. Readbytes (packsize));
HttpContext.Response.Flush ();
if (Sleep > 1) thread.sleep (sleep);
}
#endregion
}
Catch
{
ret = false;
}
Finally
{
Br. Close ();
Myfile.close ();
}
}
Catch
{
ret = false;
}
return ret;
}