zoukankan      html  css  js  c++  java
  • ASP.NET C# 文件下载速度限制

    参考了别人的代码,然后自己修改了一下

            public static bool ResponseFile(HttpContext context, string _fullPath, long _speed)
            {
                HttpRequest _Request = context.Request;
                HttpResponse _Response = context.Response;
                string strFileName = new FileInfo(_fullPath).Name;
                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;
                        
                        _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(strFileName, System.Text.Encoding.UTF8));
    
                        int pack = 10240; //10K bytes       进行拆包,每包大小                   
                        byte[] buff = new byte[pack];
                        var contentLength = br.Read(buff, 0, pack);
                        double d = 1000 / (_speed / pack); // 限速时每个包的时间
                        Stopwatch wa = new Stopwatch();
                        while (contentLength != 0)
                        {
                            if (_Response.IsClientConnected)
                            {
                                wa.Restart();
                                _Response.BinaryWrite(buff);
                                _Response.Flush();
                                contentLength = br.Read(buff, 0, pack);
                                wa.Stop();
                                if (wa.ElapsedMilliseconds < d) //如果实际带宽小于限制时间就不需要等待
                                {
                                    Thread.Sleep((int)(d - wa.ElapsedMilliseconds));
                                }
                            }
                            else
                            {
                                break;
                            }
                        }                     
                    }
                    catch
                    {
                        return false;
                    }
                    finally
                    {
                        br.Close();
                        myFile.Close();
                    }
                }
                catch
                {
                    return false;
                }
                return true;
            }
  • 相关阅读:
    [tensorflow] tf.gather使用方法
    Tensorflow Dataset.from_generator使用示例
    np.random.rand()函数
    python类
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    KNN算法
    Qt编写数据可视化大屏界面电子看板11-自定义控件
    Qt编写数据可视化大屏界面电子看板10-改造QCustomPlot
    Qt编写数据可视化大屏界面电子看板9-曲线效果
    闲谈Monaco Editor-基本使用
  • 原文地址:https://www.cnblogs.com/sgciviolence/p/3081524.html
Copyright © 2011-2022 走看看