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

📄 processupload.cs

📁 PowUpload的C#源文件,用来做大文件上传的项目
💻 CS
📖 第 1 页 / 共 3 页
字号:
namespace ElementIT.PowUpload
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web;

    internal class ProcessUpload
    {
        internal HttpApplication _app = null;
        private byte[] _Boundary;
        private int _chunksize;
        private byte[] _CloseBoundary;
        internal long _ContentLength = 0;
        private long _counter;
        private long _currentFileLength = 0;
        private byte[] _data = new byte[0];
        private int _DeficesInBoundary;
        private byte _Defis;
        private Encoding _encoding = null;
        private UploadedFile _fClass = null;
        private byte[] _FirstBoundary;
        private byte[] _FormBody = new byte[0];
        private long _FormBodyEndPos = 0;
        private long _formFieldValueStartPos = 0;
        private PartHeader _HeaderValues = new PartHeader();
        internal ElementIT.PowUpload.PowUpload _hInfo;
        private string _id;
        private long _LastWritePos = 0;
        private bool _OptimizeBoundarySeach = false;
        private long _PosBoundary = -1;
        private long _PosEndOfHeader = -1;
        private bool _PreloadWasRead = false;
        private long _read = 0;
        private long _ReadSinseLastTime = 0;
        private byte[] _TempBuff = new byte[0];
        private long _TempBuffEndPos = 0;
        private long _TempBuffSearchPos = 0;
        private string _TempFolderPath;
        internal IUploadHandle _uploadHandle = null;
        private ProcessProgress _UploadProgress = null;
        private bool _WasDisposed = false;
        private bool _WriteCurrentFile = true;
        private bool _WriteFiles = true;
        private bool IsSimulateRequest = false;
        private string SimulateBoundary = "---abc";
        private string SimulateRequest = (" name=\"suka\"\r\n\r\nbody\r\n-----abc--" + new string('-', 20));
        private byte[] SimulateRequestArray;

        internal ProcessUpload(string id)
        {
            this._id = id;
            if (UploadModule.colProcessProgress[id] == null)
            {
                throw new Exception("ProcessProgress with id=" + this._id + " not found!");
            }
            UploadModule.AddDebugInfo("ProcessUpload.Constructor(): colProcessProgress[id] found", this._id);
            this._UploadProgress = (ProcessProgress) UploadModule.colProcessProgress[id];
            this._UploadProgress._ProcessUpload = this;
        }

        private void AddDataToTempBuff()
        {
            int num = (int) Math.Max(this._TempBuffEndPos + this._read, (long) 0x10000);
            if (this._TempBuff.Length < num)
            {
                if (this._TempBuffEndPos == 0)
                {
                    this._TempBuff = new byte[num];
                }
                else
                {
                    this._TempBuff = (byte[]) Resize(this._TempBuff, (long) num);
                }
            }
            Array.Copy(this._data, (long) 0, this._TempBuff, this._TempBuffEndPos, this._read);
            this._TempBuffEndPos += this._read;
        }

        private void AddToFile(byte[] srcArray, long startIndex, long count)
        {
            if ((this._WriteFiles && this._WriteCurrentFile) && this._fClass._fs.CanWrite)
            {
                this._fClass._w.Write(srcArray, (int) startIndex, (int) count);
            }
            this._currentFileLength += count;
            this._UploadProgress._CurrentFileUploadedBytes = this._currentFileLength;
            if (this._uploadHandle != null)
            {
                byte[] destinationArray = new byte[count];
                Array.Copy(srcArray, startIndex, destinationArray, (long) 0, count);
                this._uploadHandle.WriteChunk(destinationArray, this._hInfo);
            }
        }

        private void AddToFormBody(byte[] srcArray, long startIndex, long count)
        {
            if (this._FormBody.Length < (this._FormBodyEndPos + count))
            {
                this._FormBody = (byte[]) Resize(this._FormBody, (this._FormBodyEndPos + count) + 0x7d0);
            }
            Array.Copy(srcArray, startIndex, this._FormBody, this._FormBodyEndPos, count);
            this._FormBodyEndPos += count;
        }

        internal void CancelUpload(bool closeConnection, bool deleteFiles)
        {
            UploadModule.AddDebugInfo("ProcessUpload.CancelUpload()", this._id);
            if (closeConnection)
            {
                this._UploadProgress._Status = UploadState.ConnectionClosed;
            }
            else
            {
                this._UploadProgress._Status = UploadState.Cancelled;
            }
            this.CloseAndDeleteCurrentFile();
            if (deleteFiles && (this._UploadProgress._Files != null))
            {
                UploadModule.AddDebugInfo("ProcessUpload.CancelUpload(): Dispose _Files", this._id);
                this._UploadProgress._Files.Dispose();
            }
            if (closeConnection)
            {
                this._UploadProgress._WorkerRequest.CloseConnection();
            }
        }

        private void ClearTempBuff()
        {
            this._TempBuffSearchPos = 0;
            this._LastWritePos = 0;
            this._TempBuffEndPos = 0;
        }

        private void CloseAndDeleteCurrentFile()
        {
            this.CloseCurrentFileStream();
            try
            {
                if ((this._fClass != null) && File.Exists(this._fClass._TempFileName))
                {
                    File.Delete(this._fClass._TempFileName);
                }
            }
            catch
            {
            }
        }

        private void CloseCurrentFileStream()
        {
            try
            {
                if ((this._fClass != null) && (this._fClass._w != null))
                {
                    this._fClass._w.Close();
                }
            }
            catch
            {
            }
            try
            {
                if ((this._fClass != null) && (this._fClass._fs != null))
                {
                    this._fClass._fs.Close();
                }
            }
            catch
            {
            }
        }

        private int CountDefisesinBoundary(byte[] Bound)
        {
            int num = 0;
            for (int i = 0; i < Bound.Length; i++)
            {
                if (Bound[i] == this._Defis)
                {
                    num++;
                }
                else
                {
                    return num;
                }
            }
            return num;
        }

        internal void Dispose()
        {
            this._WasDisposed = true;
            UploadModule.AddDebugInfo("ProcessUpload.Dispose(). Stack trace for current level: " + Environment.StackTrace, this._id);
            this.CloseCurrentFileStream();
        }

        private void EndFileFound()
        {
            this._fClass._ContentLength = this._currentFileLength;
            UploadModule.AddDebugInfo(string.Concat(new object[] { "End of file found: ", this._fClass._ClientFilePath, " (", this._fClass._ContentLength, ")" }), this._id);
            this.CloseCurrentFileStream();
            this._fClass._IsComplete = true;
            this._fClass._fs = null;
            this._fClass._w = null;
            this._UploadProgress._UploadedFilesCount++;
            if (this._uploadHandle != null)
            {
                this._uploadHandle.EndFileFound(this._fClass, this._hInfo);
            }
            this._UploadProgress._CurrentFileName = "";
            this._UploadProgress._CurrentFileUploadedBytes = 0;
        }

        private void EndFormFieldFound()
        {
            if ((this._FormBodyEndPos >= this._formFieldValueStartPos) && (this._formFieldValueStartPos != 0))
            {
                if (this._HeaderValues._partName != null)
                {
                    string str = this._encoding.GetString(this._FormBody, (int) this._formFieldValueStartPos, (int) (this._FormBodyEndPos - this._formFieldValueStartPos)).TrimEnd(null);
                    this._UploadProgress._FormFields.Add(this._HeaderValues._partName, str);
                    if (this._uploadHandle != null)
                    {
                        this._uploadHandle.FormFieldFound(this._HeaderValues._partName, str, this._hInfo);
                    }
                }
                this._formFieldValueStartPos = 0;
            }
        }

        private string ExractValueFromHeader(string headcontent, string name)
        {
            string str = name.ToUpper() + "=\"";
            int index = headcontent.ToUpper().IndexOf(str, 0);
            if (index == -1)
            {
                return null;
            }
            index += str.Length;
            int num2 = headcontent.IndexOf("\"", index);
            if (num2 == -1)
            {
                return null;
            }
            return headcontent.Substring(index, num2 - index);
        }

        ~ProcessUpload()
        {
            this.Dispose();
        }

        private void FinishAfterBoundary()
        {
            if (this._HeaderValues._partFilename != null)
            {
                if ((this._PosBoundary - this._LastWritePos) > 0)
                {
                    this.AddToFile(this._TempBuff, this._LastWritePos, this._PosBoundary - this._LastWritePos);
                }
                this.EndFileFound();
            }
            else
            {
                if ((this._PosBoundary - this._LastWritePos) > 0)
                {
                    this.AddToFormBody(this._TempBuff, this._LastWritePos, this._PosBoundary - this._LastWritePos);
                }
                this.EndFormFieldFound();
            }
            this._HeaderValues = new PartHeader();
        }

        private string GenerateTempFileName(string tempFolderPath)
        {
            string path = "";
            do
            {
                string str2 = Guid.NewGuid().ToString();
                path = tempFolderPath + str2 + ".tmp";
            }
            while (File.Exists(path));
            return path;
        }

        private string GetBoundary(HttpRequest request)
        {
            string str = request.ServerVariables["HTTP_Content_Type"];
            if ((str == null) || (str.Length == 0))
            {
                str = request.ServerVariables["CONTENT_TYPE"];
            }
            if ((str == null) || (str.Length == 0))
            {
                return "";
            }
            int index = str.ToLower().IndexOf("boundary=");
            if (index == -1)
            {
                return "";
            }
            string str2 = str.Substring(index + 9, str.Length - (index + 9));
            index = str2.IndexOf(",");
            if (index > 0)
            {
                str2 = str2.Substring(0, index - 1);
            }
            return str2;
        }

        private bool GetChunk()
        {
            if (!this._PreloadWasRead && !this.IsSimulateRequest)
            {
                this._data = this._UploadProgress._WorkerRequest.GetPreloadedEntityBody();
                if (this._data == null)
                {

⌨️ 快捷键说明

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