protected void Page_Load(object sender, EventArgs e)
{
}
//调用的方法
protected void DoDownLoad(long lngSpeed)
{
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "下载时显示的名称.rar", "E:\\WWWROOT\\downloadtest\\20090805.rar", lngSpeed);
if (!success)
{
Response.Write("下载文件出错!");
}
else
{
Response.Write("下载文件成功!");
}
Page.Response.End();
}
//主方法
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;
double pack = 1024;
int sleep = (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());
_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 < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch (Exception e4)
{
System.Web.HttpContext.Current.Response.Write(e4.Message);
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch (Exception e3)
{
System.Web.HttpContext.Current.Response.Write(e3.Message);
return false;
}
return true;
}
protected void Button1_Click(object sender, EventArgs e)
{
DoDownLoad(5 * 1024);
}
protected void Button2_Click(object sender, EventArgs e)
{
DoDownLoad(50 * 1024);
}
{
}
//调用的方法
protected void DoDownLoad(long lngSpeed)
{
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "下载时显示的名称.rar", "E:\\WWWROOT\\downloadtest\\20090805.rar", lngSpeed);
if (!success)
{
Response.Write("下载文件出错!");
}
else
{
Response.Write("下载文件成功!");
}
Page.Response.End();
}
//主方法
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;
double pack = 1024;
int sleep = (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());
_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 < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch (Exception e4)
{
System.Web.HttpContext.Current.Response.Write(e4.Message);
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch (Exception e3)
{
System.Web.HttpContext.Current.Response.Write(e3.Message);
return false;
}
return true;
}
protected void Button1_Click(object sender, EventArgs e)
{
DoDownLoad(5 * 1024);
}
protected void Button2_Click(object sender, EventArgs e)
{
DoDownLoad(50 * 1024);
}