Пример #1
0
        public AsyncUpload(HttpWorkerRequest wRquest)
        {
            if (wRquest == null) throw new ArgumentNullException("wRquest");
            this.uploadProcess = new UploadProcess(delegate(float f) { });
            workerRequest = wRquest;

            //当前读到的流长度
            this.preLen = workerRequest.GetPreloadedEntityBodyLength();
            //请求流的总长度
            this.totLen = workerRequest.GetTotalEntityBodyLength();
            //内容分隔符 如: -----------------------------152733254716788
            if (preLen == 0 && workerRequest.IsClientConnected() && workerRequest.HasEntityBody())
            {
                byte[] buffer = new byte[8192];
                preLen = workerRequest.ReadEntityBody(buffer, buffer.Length);
                byte[] buf = new byte[preLen];
                for (int i = 0; i < buf.Length; i++) buf[i] = buffer[i];
                this.perBodyBytes = buf;
            }
            else
                this.perBodyBytes = workerRequest.GetPreloadedEntityBody();

            this.headerBytes = this.GetBoundaryBytes(Encoding.UTF8.GetBytes(workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentType)));
            //请求流尾部分隔符 如: -----------------------------152733254716788--
            this.contentEnd = new byte[this.headerBytes.Length + 2];
            this.headerBytes.CopyTo(this.contentEnd, 0);
            this.contentEnd[this.headerBytes.Length] = 45;
            this.contentEnd[this.headerBytes.Length + 1] = 45;

            //当前流中第一个文件分隔符的位置
            int fHeaderPosition = perBodyBytes.Indexof(fileNameHeader);
            //尝试在已读取到的流中找文件尾位置
            this.fEndPosition = perBodyBytes.Indexof(contentEnd);
            if (fHeaderPosition > -1)
            {
                //先找到文件名
                IList<byte> bufList = new List<byte>();
                int i = fHeaderPosition + fileNameHeader.Length;
                while (i < perBodyBytes.Length)
                {
                    if (perBodyBytes[i] == 34) break;
                    bufList.Add(perBodyBytes[i]);
                    i++;
                }

                this.FileName = Encoding.UTF8.GetString(bufList.ToArray());//file name
                this.fPosition = perBodyBytes.Indexof(wrapBytes, i) + 4;//当前流中此文件的开始位置
                this.FileLength = this.totLen - this.fPosition;
            }
        }