Is there a way to limit file download speed in ASP.NET Core? I have a website that lets users download a large-sized file. But I want to limit the file download speed for users that are not logged in to 100kb/s. Is it possible?
Also, I've checked some solutions, but those were all for asp.net mvc.
This is my method:
public IActionResult OnGet (int id)
{
if(user is authenticated)
//download file with unlimited speed;
else
//download file with limited speed;
}
EDIT: I also have checkedthis URL. But its problem was that it waited till the byte processing was complete, then started the download with unlimited speed; Which was not what I wanted.
Maybe something like this could work.
public async Task RateLimited(Stream source, Stream target)
{
byte[] buffer = new byte[1024];
int count;
long written = 0;
while ((count = source.Read(buffer, 0, buffer.Length)) != 0)
{
target.Write(buffer, 0, count);
written += count;
if (written >= 102400L)
{
await Task.Delay(1000);
written = 0;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.