Limit download speed in C #

Source: Internet
Author: User

        <summary>///speed limit, Resource usage///</summary>//<param name= "_request" >page . Request Object </param>//<param name= "_response" >page.response object </param>//<param name= " _filename "> Download file name </param>//<param name=" _fullpath "> Download path with file name </param>//<param Nam E= "_speed" > bytes allowed to be downloaded per second </param>//<returns> return success </returns> public static bool Response File (HttpRequest _request, HttpResponse _response, String _filename, String _fullpath, Long _speed) {TR y {FileStream myFile = new FileStream (_fullpath, FileMode.Open, FileAccess.Read, FileShare.Read                Write);                BinaryReader br = new BinaryReader (myFile);                    try {_response.addheader ("accept-ranges", "bytes");                    _response.buffer = false;  Long filelength = myfile.length;                  Long startbytes = 0;                    int pack = 10240;                      int sleep = 200;                    int sleep = (int) Math.floor ((double) (+ * Pack/_speed)) + 1;                        if (_request.headers["Range"] = null) {_response.statuscode = 206; string[] Range = _request.headers["range"].                        Split (new char[] {' = ', '-'});                    Startbytes = Convert.toint64 (range[1]); } _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 (_filename, System.Text.Encoding.UTF8))                    ; Br.                    Basestream.seek (Startbytes, Seekorigin.begin);                    int maxCount = (int) Math.floor ((Double) ((filelength-startbytes)/pack) + 1;                        for (int i = 0; i < MaxCount; i++) {if (_response.isclientconnected) {_response.binarywrite (br.                            Readbytes (pack));                        Thread.Sleep (Sleep);                        } else {i = MaxCount;                }}} catch {return false; } finally {Br.                    Close ();                Myfile.close ();    }} catch {            return false;        } return true;  }

  

Limit download speed in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.