⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processupload.cs

📁 PowUpload的C#源文件,用来做大文件上传的项目
💻 CS
📖 第 1 页 / 共 3 页
字号:
                {
                    return true;
                }
                num2++;
            }
            return false;
        }

        private void NewFileFound(PartHeader headervalues)
        {
            UploadModule.AddDebugInfo("Found file: " + headervalues._partFilename, this._id);
            this._UploadProgress._CurrentFileName = headervalues._partFilename;
            this._currentFileLength = 0;
            this._UploadProgress._CurrentFileUploadedBytes = 0;
            this._fClass = new UploadedFile();
            this._fClass._ClientFilePath = headervalues._partFilename;
            this._fClass._IsComplete = false;
            this._fClass._FieldName = headervalues._partName;
            this._fClass._ContentType = headervalues._partContentType;
            this._fClass._TempFileName = this.GenerateTempFileName(this._TempFolderPath);
            this._UploadProgress._Files.AddFile(this._fClass._FieldName, this._fClass);
            this._WriteCurrentFile = true;
            if (this._uploadHandle != null)
            {
                this._WriteCurrentFile = this._uploadHandle.NewFileFound(this._fClass, this._hInfo);
            }
            if (this._WriteFiles && this._WriteCurrentFile)
            {
                int num;
                UploadModule.AddDebugInfo("File will be saved on disk", this._id);
                if (this._chunksize > 0x1000)
                {
                    num = this._chunksize;
                }
                else
                {
                    num = 0x1000;
                }
                this._fClass._fs = new FileStream(this._fClass._TempFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, num);
                this._fClass._w = new BinaryWriter(this._fClass._fs);
                this._fClass._w.BaseStream.Position = 0;
            }
            else
            {
                UploadModule.AddDebugInfo("File rejected.", this._id);
                this._fClass._Rejected = true;
            }
        }

        private void OnEndReadRequest()
        {
            this.CloseCurrentFileStream();
            if ((this._UploadProgress != null) && (this._UploadProgress._WorkerRequest != null))
            {
                if (this._UploadProgress._WorkerRequest.IsClientConnected())
                {
                    this._UploadProgress._UploadComplete = true;
                    this._UploadProgress._Status = UploadState.Complete;
                }
                else
                {
                    this._UploadProgress._UploadComplete = true;
                    this._UploadProgress._Status = UploadState.Cancelled;
                    if (this._uploadHandle != null)
                    {
                        this._uploadHandle.CancelRequest(this._hInfo);
                    }
                }
            }
        }

        internal void ParseRequest()
        {
            bool iscloseboundary = false;
            long num = -1;
            bool flag2 = true;
            bool flag3 = false;
            bool flag4 = false;
            bool flag5 = true;
            bool flag6 = false;
            try
            {
                UploadModule.AddDebugInfo("ProcessUpload.ParseRequest(): start", this._id);
                this.InitConfigVars();
                this._encoding = this.GetEncoding(this._app.Context.Request);
                if (this.IsSimulateRequest)
                {
                    this.SimulateRequestArray = this._encoding.GetBytes(this.SimulateRequest);
                }
                this.InitBoundaries();
                this._UploadProgress._bwc = new BandwidthControl(this._id, this._chunksize, (int) Settings.GetValue(this._id, "bandwidthLimit"));
                if (this.IsSimulateRequest)
                {
                    this._ContentLength = this.SimulateRequestArray.Length;
                }
                this.InitProgressState();
                if (!UploadModule.IsValidMaxRequestLength(this._ContentLength, this._id))
                {
                    this._UploadProgress._Status = UploadState.Rejected;
                    this._WriteFiles = false;
                    this._UploadProgress._lastError = new HttpException("Maximum request length exceeded.");
                }
                this.GetChunk();
                this.AddDataToTempBuff();
                UploadModule.AddDebugInfo("ProcessUpload.ParseRequest(): Preloaded bytes:" + this._read, this._id);
                while (true)
                {
                    if (this.IsCancelled() || this._WasDisposed)
                    {
                        break;
                    }
                    if (!flag2)
                    {
                        if (!this.GetChunk())
                        {
                            break;
                        }
                        this.AddDataToTempBuff();
                    }
                    flag2 = false;
                    if (flag5)
                    {
                        long num2 = this.InStrB_Boundary(this._TempBuffSearchPos, this._TempBuff, ref iscloseboundary, this._TempBuffEndPos, this._FirstBoundary);
                        if (num2 != -1)
                        {
                            this._PosBoundary = num2;
                            flag5 = false;
                            this._TempBuffSearchPos = num2 + this._FirstBoundary.Length;
                            flag6 = true;
                            flag2 = true;
                        }
                    }
                    if (flag3)
                    {
                        long num3;
                        if (num != -1)
                        {
                            num3 = num;
                        }
                        else
                        {
                            num3 = this.InStrB_Boundary(this._TempBuffSearchPos, this._TempBuff, ref iscloseboundary, this._TempBuffEndPos, this._Boundary);
                        }
                        if (num3 != -1)
                        {
                            this._PosBoundary = num3;
                            this.FinishAfterBoundary();
                            if (iscloseboundary)
                            {
                                this.AddToFormBody(this._CloseBoundary, (long) 0, (long) this._CloseBoundary.Length);
                                break;
                            }
                            this._TempBuffSearchPos = this._PosBoundary + this._Boundary.Length;
                            flag3 = false;
                            flag6 = true;
                            flag4 = false;
                            flag2 = true;
                        }
                    }
                    if (flag6)
                    {
                        byte[] searcharr = this.StrToByteArray("\r\n");
                        long startpos = this.InStrB(this._TempBuffSearchPos, this._TempBuff, searcharr, this._TempBuffEndPos);
                        if (startpos != -1)
                        {
                            searcharr = this.StrToByteArray("\r\n\r\n");
                            this._PosEndOfHeader = this.InStrB(startpos, this._TempBuff, searcharr, this._TempBuffEndPos);
                            if (this._PosEndOfHeader != -1)
                            {
                                this._PosEndOfHeader += 4;
                                this._TempBuffSearchPos = this._PosEndOfHeader;
                                flag4 = true;
                                flag3 = true;
                                flag6 = false;
                                this.ProccesHeader(startpos);
                                this._LastWritePos = this._PosEndOfHeader;
                                flag2 = true;
                            }
                            else if ((this._TempBuffEndPos - startpos) > 0xf4240)
                            {
                                flag4 = true;
                                flag3 = true;
                                flag6 = false;
                            }
                        }
                        else if ((this._TempBuffEndPos - this._TempBuffSearchPos) > 0xf4240)
                        {
                            flag4 = true;
                            flag3 = true;
                            flag6 = false;
                        }
                    }
                    num = -1;
                    if (flag4 && ((this._TempBuffEndPos - this._LastWritePos) > 0))
                    {
                        if (flag2)
                        {
                            num = this.InStrB_Boundary(this._TempBuffSearchPos, this._TempBuff, ref iscloseboundary, this._TempBuffEndPos, this._Boundary);
                        }
                        if ((num == -1) && (!this.IsPartOfBoundaryAtEndofTempBuff() || ((this._TempBuffEndPos - this._LastWritePos) > Math.Max(0x989680, this._chunksize * 3))))
                        {
                            if (this._HeaderValues._partFilename != null)
                            {
                                this.AddToFile(this._TempBuff, this._LastWritePos, this._TempBuffEndPos - this._LastWritePos);
                            }
                            else
                            {
                                this.AddToFormBody(this._TempBuff, this._LastWritePos, this._TempBuffEndPos - this._LastWritePos);
                            }
                            this.ClearTempBuff();
                        }
                    }
                }
                if (!iscloseboundary)
                {
                    this.AddToFormBody(this._CloseBoundary, (long) 0, (long) this._CloseBoundary.Length);
                }
                if (!this.IsCancelled() && !this._WasDisposed)
                {
                    if ((bool) Settings.GetValue(this._id, "passForm"))
                    {
                        this._FormBody = (byte[]) Resize(this._FormBody, (long) ((int) this._FormBodyEndPos));
                    }
                    else
                    {
                        this._FormBody = new byte[0];
                    }
                    UploadModule.AddDebugInfo("ProcessUpload.ParseRequest(): Pass to Worker Request FormBody. Length=" + this._FormBody.Length, this._id);
                    Common.SetFormBodyAtWorkerRequest(this._UploadProgress._WorkerRequest, this._FormBody, this._app);
                }
                else
                {
                    this._FormBody = new byte[0];
                    Common.SetFormBodyAtWorkerRequest(this._UploadProgress._WorkerRequest, this._FormBody, this._app);
                }
            }
            catch (Exception exception)
            {
                if (!this.IsCancelled() && !this._WasDisposed)
                {
                    UploadModule.AddDebugInfo("ProcessUpload.ParseRequest(): Exception: " + exception.ToString(), this._id);
                    this.SetEndTime();
                    bool flag7 = true;
                    if (this._uploadHandle != null)
                    {
                        flag7 = this._uploadHandle.ParseError(exception, this._hInfo);
                    }
                    this.CloseAndDeleteCurrentFile();
                    this._UploadProgress._Status = UploadState.Error;
                    this._UploadProgress._lastError = exception;
                    Exception exception2 = new Exception("ProcessUpload exception catch: " + exception.Message, exception);
                    if (flag7)
                    {
                        throw exception2;
                    }
                }
            }
            this.SetEndTime();
            this.OnEndReadRequest();
        }

        private void ProccesHeader(long PosStartOfHeader)
        {
            this._HeaderValues = this.GetPartHeader(this._TempBuff, PosStartOfHeader, this._PosEndOfHeader);
            if (this._HeaderValues._partFilename != null)
            {
                this.NewFileFound(this._HeaderValues);
                this.AddToFormBody(this._TempBuff, this._PosBoundary, this._PosEndOfHeader - this._PosBoundary);
                if (this._HeaderValues._partFilename != "")
                {
                    string str = string.Format("The original file is in the temp folder. Full path of the file: {0}", this._fClass._TempFileName);
                    if (!this._WriteFiles || !this._WriteCurrentFile)
                    {
                        str = string.Format("The original file was not saved.", "Maximum request length exceeded.");
                    }
                    this.AddToFormBody(this.StrToByteArray(str), (long) 0, (long) str.Length);
                }
            }
            else
            {
                this.AddToFormBody(this._TempBuff, this._PosBoundary, this._PosEndOfHeader - this._PosBoundary);
                this._formFieldValueStartPos = this._FormBodyEndPos;
            }
        }

        private static Array Resize(Array array, long newsize)
        {
            Array destinationArray = Array.CreateInstance(array.GetType().GetElementType(), new long[] { newsize });
            Array.Copy(array, 0, destinationArray, 0, Math.Min(destinationArray.Length, array.Length));
            return destinationArray;
        }

        private void SetEndTime()
        {
            this._UploadProgress._EndTime = DateTime.Now;
        }

        private void SetProgressState(long counter)
        {
            this._UploadProgress._UploadedBytes = counter;
            if (DateTime.Now.Subtract(this._UploadProgress._LastTime).TotalMilliseconds >= 900)
            {
                this._UploadProgress._TimeSinseLastTime = DateTime.Now.Subtract(this._UploadProgress._LastTime);
                this._UploadProgress._LastTime = DateTime.Now;
                this._UploadProgress.SetReadSinseLastTime(this._ReadSinseLastTime);
                this._ReadSinseLastTime = 0;
            }
            this._UploadProgress._SecondsDone = DateTime.Now.Subtract(this._UploadProgress._StartTime).TotalSeconds;
        }

        private byte[] StrToByteArray(string str)
        {
            return this._encoding.GetBytes(str);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -