public void StreamToDisk(IServiceProvider provider, Encoding encoding, string rootPath)
        {
            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath);
            }

            //this._encoding = this.GetEncodingFromHeaders();
            //if (this._encoding == null)

            var buffer = new byte[8192];

            if (!_workerRequest.HasEntityBody())
            {
                return;
            }

            var total     = _workerRequest.GetTotalEntityBodyLength();
            var preloaded = _workerRequest.GetPreloadedEntityBodyLength();
            var loaded    = preloaded;

            SetByteMarkers(_workerRequest, encoding);

            var body = _workerRequest.GetPreloadedEntityBody();

            if (body == null) // IE normally does not preload
            {
                body      = new byte[8192];
                preloaded = _workerRequest.ReadEntityBody(body, body.Length);
                loaded    = preloaded;
            }

            var text            = encoding.GetString(body);
            var matchcollection = _filename.Matches(text);
            var fileName        = matchcollection[0].Groups[2].Value;

            fileName = Path.GetFileName(fileName); // IE captures full user path; chop it

            SavedFileName = FileCheckRename(rootPath, fileName);
            var path = Path.Combine(rootPath, SavedFileName);

            var files = new List <string> {
                fileName
            };
            var stream = new FileStream(path, FileMode.Create);

            if (preloaded > 0)
            {
                stream = ProcessHeaders(body, stream, encoding, preloaded, files, rootPath);
            }

            // Used to force further processing (i.e. redirects) to avoid buffering the files again
            var workerRequest = new StaticWorkerRequest(_workerRequest, body);
            var field         = HttpContext.Current.Request.GetType().GetField("_wr", BindingFlags.NonPublic | BindingFlags.Instance);

            field.SetValue(HttpContext.Current.Request, workerRequest);

            if (!_workerRequest.IsEntireEntityBodyIsPreloaded())
            {
                var received = preloaded;
                while (total - received >= loaded && _workerRequest.IsClientConnected())
                {
                    loaded = _workerRequest.ReadEntityBody(buffer, buffer.Length);
                    stream = ProcessHeaders(buffer, stream, encoding, loaded, files, rootPath);

                    received += loaded;
                }

                var remaining = (total == preloaded) ? preloaded : total - received;
                buffer = new byte[remaining];

                loaded = _workerRequest.ReadEntityBody(buffer, remaining);
                stream = ProcessHeaders(buffer, stream, encoding, loaded, files, rootPath);
            }

            stream.Flush();
            stream.Close();
            stream.Dispose();
        }
        public void StreamToDisk(IServiceProvider provider, Encoding encoding, string rootPath)
        {
            if (!Directory.Exists(rootPath))
                Directory.CreateDirectory(rootPath);

            //this._encoding = this.GetEncodingFromHeaders();
            //if (this._encoding == null)

            var buffer = new byte[8192];

            if (!_workerRequest.HasEntityBody())
            {
                return;
            }

            var total = _workerRequest.GetTotalEntityBodyLength();
            var preloaded = _workerRequest.GetPreloadedEntityBodyLength();
            var loaded = preloaded;

            SetByteMarkers(_workerRequest, encoding);

            var body = _workerRequest.GetPreloadedEntityBody();
            if (body == null) // IE normally does not preload
            {
                body = new byte[8192];
                preloaded = _workerRequest.ReadEntityBody(body, body.Length);
                loaded = preloaded;
            }

            var text = encoding.GetString(body);
            var matchcollection = _filename.Matches(text);
            var fileName = matchcollection[0].Groups[2].Value;
            fileName = Path.GetFileName(fileName); // IE captures full user path; chop it

            SavedFileName = FileCheckRename(rootPath, fileName);
            var path = Path.Combine(rootPath, SavedFileName);

            var files = new List<string> { fileName };
            var stream = new FileStream(path, FileMode.Create);

            if (preloaded > 0)
            {
                stream = ProcessHeaders(body, stream, encoding, preloaded, files, rootPath);
            }

            // Used to force further processing (i.e. redirects) to avoid buffering the files again
            var workerRequest = new StaticWorkerRequest(_workerRequest, body);
            var field = HttpContext.Current.Request.GetType().GetField("_wr", BindingFlags.NonPublic | BindingFlags.Instance);
            field.SetValue(HttpContext.Current.Request, workerRequest);

            if (!_workerRequest.IsEntireEntityBodyIsPreloaded())
            {
                var received = preloaded;
                while (total - received >= loaded && _workerRequest.IsClientConnected())
                {
                    loaded = _workerRequest.ReadEntityBody(buffer, buffer.Length);
                    stream = ProcessHeaders(buffer, stream, encoding, loaded, files, rootPath);

                    received += loaded;
                }

                var remaining = (total == preloaded) ? preloaded : total - received;
                buffer = new byte[remaining];

                loaded = _workerRequest.ReadEntityBody(buffer, remaining);
                stream = ProcessHeaders(buffer, stream, encoding, loaded, files, rootPath);
            }

            stream.Flush();
            stream.Close();
            stream.Dispose();
        }