[英]How to limit speed downloading file in asp.net mvc 3
I have read this article on codeproject, but i dont know how to apply it to asp.net mvc 3 http://www.codeproject.com/Articles/18243/Bandwidth-throttling 我在codeproject上读过这篇文章,但我不知道如何将它应用到asp.net mvc 3 http://www.codeproject.com/Articles/18243/Bandwidth-throttling
Here is code for downloading a file in my project 这是在我的项目中下载文件的代码
public ActionResult GetFile(int id)
{
var f = FileAcc.GetInfo(id);
var templateStr = new FileStream(Server.MapPath(f.file_url), FileMode.Open);
return File(templateStr, f.file_name);
}
Plz support me on this issues, thanks a lot! Plz在这个问题上支持我,非常感谢!
Simply include ThrottledStream.cs
in your project and replace your GetFile method with following - 只需在项目中包含ThrottledStream.cs
,并用以下代码替换GetFile方法 -
public ActionResult GetFile(int id) {
var f = FileAcc.GetInfo(id);
int bufferSize = 1024, bps = 1024;
using (FileStream sourceStream = new FileStream(Server.MapPath(f.file_url), FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize)) {
using (Born2Code.Net.ThrottledStream destinationStream = new Born2Code.Net.ThrottledStream(Response.OutputStream, bps)) {
byte[] buffer = new byte[bufferSize];
int readCount = sourceStream.Read(buffer, 0, bufferSize);
Response.Buffer = false;
while (readCount > 0) {
destinationStream.Write(buffer, 0, readCount);
readCount = sourceStream.Read(buffer, 0, bufferSize);
}
}
}
return new EmptyResult();
}
and tweak bps per your need. 并根据您的需要调整bps。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.