Пример #1
0
        public static bool DownloadFile(HttpRequest _Request, HttpResponse _Response, string _fullPath, long _speed)
        {
            string fileName = DirFile.GetFileName(false, _fullPath);

            try
            {
                FileStream   fileStream   = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader binaryReader = new BinaryReader((Stream)fileStream);
                try
                {
                    _Response.AddHeader("Accept-Ranges", "bytes");
                    _Response.Buffer = false;
                    long   length = fileStream.Length;
                    long   offset = 0;
                    double num1   = 10240.0;
                    int    millisecondsTimeout = (int)Math.Floor(1000.0 * num1 / (double)_speed) + 1;
                    if (_Request.Headers["Range"] != null)
                    {
                        _Response.StatusCode = 206;
                        offset = Convert.ToInt64(_Request.Headers["Range"].Split(new char[2]
                        {
                            '=',
                            '-'
                        })[1]);
                    }
                    _Response.AddHeader("Content-Length", (length - offset).ToString());
                    _Response.AddHeader("Connection", "Keep-Alive");
                    _Response.ContentType = "application/octet-stream";
                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
                    binaryReader.BaseStream.Seek(offset, SeekOrigin.Begin);
                    int num2 = (int)Math.Floor((double)(length - offset) / num1) + 1;
                    for (int index = 0; index < num2; ++index)
                    {
                        if (_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(binaryReader.ReadBytes(int.Parse(num1.ToString())));
                            Thread.Sleep(millisecondsTimeout);
                        }
                        else
                        {
                            index = num2;
                        }
                    }
                }
                catch
                {
                    return(false);
                }
                finally
                {
                    binaryReader.Close();
                    fileStream.Close();
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
 public static string GetFileName(string filePath)
 {
     return(DirFile.GetFileName(false, filePath));
 }