简体   繁体   English

限制 ASP.NET Core 7 中的下载速度

[英]Limit download speed in ASP.NET Core 7

Is there a way to limit file download speed in ASP.NET Core?有没有办法限制 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.但是我想限制未登录用户的文件下载速度为100kb/s。 Is it possible?可能吗?

Also, I've checked some solutions, but those were all for asp.net mvc.另外,我检查了一些解决方案,但这些都是针对 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.编辑:我也检查了这个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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM