在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
这个是一个Restful上传文件的实现方法,我们可以在上传过程中通过线程等待来现实限速功能 /// <summary> /// 上传 /// </summary> /// <param name="fileName"></param> /// <param name="fileStream"></param> public void FileUpload(string fileName, Stream fileStream) { FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create); int _speed = 1024 * 1024; // 1M/S int pack = 10240; //10K bytes byte[] bytearray = new byte[pack]; double limit = 1000 / (_speed / pack); // 限速时每个包的时间 int bytesRead, totalBytesRead = 0; do { Stopwatch wa = new Stopwatch(); wa.Restart(); bytesRead = fileStream.Read(bytearray, 0, bytearray.Length); totalBytesRead += bytesRead; wa.Stop(); if (wa.ElapsedMilliseconds < limit) //如果实际带宽小于限制时间就不需要等待 { Thread.Sleep((int)(limit - wa.ElapsedMilliseconds)); } } while (bytesRead > 0); fileToupload.Write(bytearray, 0, bytearray.Length); fileToupload.Close(); fileToupload.Dispose(); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论