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

📄 uploadedfile.cs

📁 PowUpload的C#源文件,用来做大文件上传的项目
💻 CS
字号:
namespace ElementIT.PowUpload
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class UploadedFile
    {
        internal string _ClientFilePath = "";
        internal long _ContentLength = 0;
        internal string _ContentType = "";
        private object _CustomData = null;
        internal string _FieldName = "";
        internal FileStream _fs = null;
        private bool _ImageWasRead = false;
        private int _ImgHeight = 0;
        private int _ImgWidth = 0;
        private Stream _InputStream = null;
        internal bool _IsComplete = false;
        private bool _IsImage = false;
        internal bool _Rejected = false;
        internal string _TempFileName = "";
        internal BinaryWriter _w = null;

        internal UploadedFile()
        {
        }

        public void CloseInputStream()
        {
            if (this._InputStream != null)
            {
                this._InputStream.Close();
            }
        }

        public void CopyTo(string filename)
        {
            this.CopyTo(filename, false);
        }

        public void CopyTo(string filename, bool overwrite)
        {
            if ((this._TempFileName != null) && (this._TempFileName != ""))
            {
                if ((this._ClientFilePath == null) || (this._ClientFilePath == ""))
                {
                    throw new IOException("The client file path is empty (the file was not selected in the form). You cannot run this method.");
                }
                if (overwrite)
                {
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                }
                else if (File.Exists(filename))
                {
                    throw new IOException(string.Format("File {0} already exists.", filename));
                }
                File.Copy(this._TempFileName, filename);
            }
        }

        public void Dispose()
        {
            UploadModule.AddDebugInfo("UploadedFile.Dispose(): _TempFileName=" + this._TempFileName);
            try
            {
                if (this._fs != null)
                {
                    this._fs.Close();
                }
            }
            catch
            {
            }
            try
            {
                if (this._w != null)
                {
                    this._w.Close();
                }
            }
            catch
            {
            }
            try
            {
                if (this._InputStream != null)
                {
                    this._InputStream.Close();
                }
            }
            catch
            {
            }
            try
            {
                if (((this._TempFileName != null) && (this._TempFileName != "")) && File.Exists(this._TempFileName))
                {
                    File.Delete(this._TempFileName);
                }
            }
            catch
            {
            }
        }

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

        public string GetHashMD5()
        {
            if (!this._IsComplete)
            {
                throw new Exception("Method or property can't be executed while file upload not complete!");
            }
            if ((this._TempFileName != null) && (this._TempFileName != ""))
            {
                try
                {
                    StringBuilder builder = new StringBuilder();
                    MD5 md = new MD5CryptoServiceProvider();
                    FileStream inputStream = new FileStream(this._TempFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    byte[] buffer = null;
                    try
                    {
                        buffer = md.ComputeHash(inputStream);
                    }
                    finally
                    {
                        inputStream.Close();
                    }
                    if (buffer != null)
                    {
                        foreach (byte num in buffer)
                        {
                            builder.Append(num.ToString("x2"));
                        }
                    }
                    return builder.ToString();
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        public static string GetSafeName(string filepath)
        {
            int num = filepath.LastIndexOf(@"\");
            int num2 = filepath.LastIndexOf("/");
            string str = "";
            if ((num == -1) && (num2 == -1))
            {
                str = filepath;
            }
            else if (num > num2)
            {
                str = filepath.Substring(num + 1, (filepath.Length - num) - 1);
            }
            else
            {
                str = filepath.Substring(num2 + 1, (filepath.Length - num2) - 1);
            }
            char[] chArray = new char[] { '?', '*', '\\', '/', ':' };
            char[] invalidPathChars = Path.InvalidPathChars;
            char[] array = new char[invalidPathChars.Length + chArray.Length];
            chArray.CopyTo(array, 0);
            invalidPathChars.CopyTo(array, chArray.Length);
            for (int i = 0; i <= (array.Length - 1); i++)
            {
                int index;
                do
                {
                    index = str.IndexOf(array[i]);
                    if (index != -1)
                    {
                        str = str.Remove(index, 1);
                    }
                }
                while (index != -1);
            }
            if (str.Length > 0xff)
            {
                str = str.Substring(str.Length - 0xff, 0xff);
            }
            return str;
        }

        public void SaveAs(string filename)
        {
            this.SaveAs(filename, false);
        }

        public void SaveAs(string filename, bool overwrite)
        {
            if (!this._IsComplete)
            {
                throw new Exception("Method or property can't be executed while file upload not complete!");
            }
            if ((this._ClientFilePath == null) || (this._ClientFilePath == ""))
            {
                throw new IOException("The client file path is empty (the file was not selected in the form). You cannot run this method.");
            }
            if (overwrite)
            {
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }
            else if (File.Exists(filename))
            {
                throw new IOException(string.Format("File {0} already exists. Set the overwrite parameter to \"true\" to overwtite the file.", filename));
            }
            File.Move(this._TempFileName, filename);
        }

        private void SetImgParams()
        {
            if (!this._ImageWasRead && !this._Rejected)
            {
                Image image = null;
                try
                {
                    image = Image.FromFile(this._TempFileName);
                    this._ImgHeight = image.Height;
                    this._ImgWidth = image.Width;
                    this._IsImage = true;
                }
                catch
                {
                    this._ImgHeight = 0;
                    this._ImgWidth = 0;
                    this._IsImage = false;
                }
                finally
                {
                    this._ImageWasRead = true;
                    if (image != null)
                    {
                        image.Dispose();
                    }
                }
            }
        }

        public string ClientFilePath
        {
            get
            {
                return this._ClientFilePath;
            }
        }

        public long ContentLength
        {
            get
            {
                return this._ContentLength;
            }
        }

        public string ContentType
        {
            get
            {
                return this._ContentType;
            }
        }

        public object CustomData
        {
            get
            {
                return this._CustomData;
            }
            set
            {
                this._CustomData = value;
            }
        }

        public string Extension
        {
            get
            {
                if ((this._ClientFilePath != "") && (this._ClientFilePath != null))
                {
                    try
                    {
                        return Path.GetExtension(GetSafeName(this._ClientFilePath));
                    }
                    catch
                    {
                    }
                }
                return "";
            }
        }

        public string FieldName
        {
            get
            {
                return this._FieldName;
            }
        }

        public string FileName
        {
            get
            {
                return this._ClientFilePath;
            }
        }

        public int ImageHeight
        {
            get
            {
                if (!this._IsComplete)
                {
                    throw new Exception("Method or property can't be executed while file upload not complete!");
                }
                this.SetImgParams();
                return this._ImgHeight;
            }
        }

        public int ImageWidth
        {
            get
            {
                if (!this._IsComplete)
                {
                    throw new Exception("Method or property can't be executed while file upload not complete!");
                }
                this.SetImgParams();
                return this._ImgWidth;
            }
        }

        public Stream InputStream
        {
            get
            {
                if ((this._Rejected || (this._ClientFilePath == null)) || ((this._ClientFilePath == "") || !File.Exists(this._TempFileName)))
                {
                    return null;
                }
                if ((this._InputStream == null) && this._IsComplete)
                {
                    this._InputStream = new FileStream(this._TempFileName, FileMode.Open);
                }
                else if ((this._InputStream == null) && !this._IsComplete)
                {
                    this._InputStream = new FileStream(this._TempFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                }
                return this._InputStream;
            }
        }

        public bool IsComplete
        {
            get
            {
                return this._IsComplete;
            }
        }

        public bool IsImage
        {
            get
            {
                if (!this._IsComplete)
                {
                    throw new Exception("Method or property can't be executed while file upload not complete!");
                }
                this.SetImgParams();
                return this._IsImage;
            }
        }

        public bool Rejected
        {
            get
            {
                return this._Rejected;
            }
        }

        public string SafeFileName
        {
            get
            {
                if ((this._ClientFilePath != null) && (this._ClientFilePath != ""))
                {
                    return GetSafeName(this._ClientFilePath);
                }
                return "";
            }
        }

        public string TempFileName
        {
            get
            {
                return this._TempFileName;
            }
            set
            {
                this._TempFileName = value;
            }
        }
    }
}

⌨️ 快捷键说明

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