13
我有这样的代码,当只有1下载的同时运行如何在下载文件时限制带宽并允许多次下载?
using (System.IO.FileStream fs = System.IO.File.OpenRead(@"C:\HugeFile.GBD"))
{
using (System.IO.BinaryReader br = new System.IO.BinaryReader(fs))
{
Response.AddHeader("Cache-control", "private");
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", fs.Length.ToString());
Response.AddHeader("Content-Disposition", "filename=HugeFile.GBD");
Response.Flush();
float kbs = 20f;
while (fs.Position < fs.Length)
{
if (!Response.IsClientConnected)
break;
byte[] bytes = br.ReadBytes((int)Math.Truncate(1024 * kbs));
char[] c = UTF8Encoding.Default.GetChars(bytes);
Response.Write(c, 0, c.Length);
Response.Flush();
System.Threading.Thread.Sleep(1000);
}
Response.Flush();
}
}
但是,如果我做两个同时连接(开始在同一个浏览器第二版),第二个将不执行该工作直到第一次结束。
使用线程或错误任务结果时添加页眉到Response
...
我怎样才能使这样我就可以在同一时间在同一个浏览器上执行2+下载?
也许看看做这个['任务'](http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.startnew.aspx)并旋转你需要的。此外,您可以查看[本网站](http://www.codethinked.com/net-40-and-systemthreadingtasks)以获取更多示例。 –
我可能是错的,但我认为你不必为此使用多线程。服务器应该自动处理,即使它是一个巨大的文件。你如何测试它?如果他们从2台不同的机器上进行测试并且仍然存在相同的问题呢? – urlreader
使用任务或线程,它在'Response.AddHeader(“Cache-control”,“private”)失败;' 'ArgumentException,值不在预期范围内。' – BrunoLM