有没有谁可以给个线程控制上传或下载速度的例子,谢谢了

carl2004 2005-05-18 03:33:52
ASP.net(C#)中如何用线程控制用户上传或下载的速度.
...全文
148 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzdjoa 2005-08-03
  • 打赏
  • 举报
回复
mark
cathylang 2005-06-29
  • 打赏
  • 举报
回复
mark
carl2004 2005-05-19
  • 打赏
  • 举报
回复
谢谢,我研究一下,等会给分。
gengxin_914 2005-05-18
  • 打赏
  • 举报
回复
up
jenet 2005-05-18
  • 打赏
  • 举报
回复
up
wq2000 2005-05-18
  • 打赏
  • 举报
回复
我也想知道up
mba9001 2005-05-18
  • 打赏
  • 举报
回复
mark
hchxxzx 2005-05-18
  • 打赏
  • 举报
回复
ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)

原稿CSDN网友 maling(***) ( )


// 输出硬盘文件,提供下载
// 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数
// 返回是否成功
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次 即5*10K bytes每秒
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());
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 < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i=maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}


调用例

Page.Response.Clear();

bool success = ResponseFile(Page.Request, Page.Response, "filename", @"C:\download.date", 1024000);

if(!success)
Response.Write("下载文件出错!");

Page.Response.End();
carl2004 2005-05-18
  • 打赏
  • 举报
回复
顶上去。不要沉了。

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧