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

📄 uploadfile.cs

📁 上传的控件
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace WUSGControl.Web.Upload
{
    /// <summary>
    /// 定义单个上传文件类
    /// </summary>
    public class UploadFile
    {
        //私有自段
        private long _filesize;
        private string _contenttype;
        private string _filename;
        private string _filepath;
        private string _clientname;      

        #region 属性
        /// <summary>
        /// 文件的总长度
        /// </summary>
        public long FileSize
        {
            get{ return _filesize; }
        }

        /// <summary>
        /// Gets the MIME content type of the uploaded file
        /// </summary>
        public string ContentType
        {
            get { return _contenttype; }      
           
        }
        /// <summary>
        /// 上传文件的名字
        /// </summary>
        public string FileName
        {
            get 
            {
                _filename = Regex.Replace(_filename, @"\s", ""); 
                return _filename;
            }
        }
        /// <summary>
        /// 客户端上文件的路径和名字
        /// </summary>
        public string ClientFullPathName
        {
            get { return _clientname; }
        }
        /// <summary>
        /// 文件的扩展名
        /// </summary>
        public string ExtendName
        {
            get { return _filename.Substring(_filename.LastIndexOf(".")); }
        }
        /// <summary>
        /// 含路径的新文件,就是GUID产生的那个文件, 在这个文件上加它的真实名字,以免重复.
        /// </summary>
        public string NewFileName
        {
            get { return Path.GetFileName(this._filepath);}
        }

        #endregion

        /// <summary>
        /// 上传文件的构造类
        /// 其字符格式如下:
        /// "Content-Type: text/plain;filename=\"C:\\Documents and Settings\\Administrator\\桌面\\已被请求的正文.txt\";
        /// filepath=\"855ef86f-3f14-4ac4-9b26-10ecaa934d44.txt\""
        /// </summary>
        /// <param name="i_name">从上传文件头中分析的文件的名字</param>
        public UploadFile(string name)
        {
            if (name == null || name == string.Empty)
            {
                throw new ArgumentNullException("Name", "Name can not be null!");
            }

            this._contenttype = string.Empty;
            this._clientname = string.Empty;
            this._filename = string.Empty;
            this._filepath = string.Empty;
            this._filesize = 0;

            string m_content = string.Empty;

            if (IsContentHeader(name))
            {
                m_content = name;
            }
            else if(IsContentHeader(Utils.GetContext().Request[name]))
            {
                m_content = Utils.GetContext().Request[name];
            }

            if (m_content == null || m_content == string.Empty)
            {
                return;
            }

            //解析name中的字符串,写到各私有变量中
            //Get file info from content.
            string[] contentArray = m_content.Split(';');

            this._contenttype = contentArray[0];
            this._contenttype = this._contenttype.Substring(14, (this._contenttype.Length - 14));
            this._filename = contentArray[1];
            this._filename = this._filename.Substring(10, (this._filename.Length - 11));
            this._clientname = _filename;
            this._filename = Path.GetFileName(this._clientname);
            
            this._filepath = contentArray[2];
            this._filepath = this._filepath.Substring(10, (this._filepath.Length - 11));

            string uploadFolder = Utils.GetUploadFolder();
            this._filepath = Path.Combine(uploadFolder, this._filepath);

            try
            {
                this._filesize = new FileInfo(this._filepath).Length;//文件的实际大小
            }
            catch (Exception exception)
            {
                string uploadGuid = Utils.GetContext().Request["Sunrise_Web_Upload_UploadGUID"];

                if (uploadGuid != null)
                {
                    Utils.GetContext().Application.Remove(("_UploadGUID_" + uploadGuid));
                }

                throw exception;
            }
        }

        /// <summary>
        /// 获得当前上传文件的流,以作为发邮件的附件附加方式 
        /// </summary>
        /// <returns></returns>
        public Stream GetFileStream()
        {
            FileInfo f = new FileInfo(this._filepath);
            
            byte[] filebytes = null;
            if (f.Exists)
            {
                filebytes = new byte[f.Length];
                f.OpenRead().Read(filebytes, 0, filebytes.Length);
            }
            Stream stream = new MemoryStream(filebytes);
            return stream;
        }

        /// <summary>
        /// 获取文件的字节
        /// </summary>
        /// <returns></returns>
        public byte[] GetFileBytes()
        {
            FileInfo f = new FileInfo(this._filepath);

            //byte[] filebytes = null;
            //if (f.Exists)
            //{
            //    filebytes = new byte[f.Length];
            //    f.OpenRead().Read(filebytes, 0, filebytes.Length);
            //    f.OpenRead().Flush();
            //    f.OpenRead().Close();
            //    f.Delete();
            //}
            byte[] buffer = null;
            if (f.Exists)
            {
                FileStream a = new FileStream(this._filepath, FileMode.Open, FileAccess.Read);
                buffer = new byte[a.Length];
                a.Read(buffer, 0, buffer.Length);
                //memberbinfoacce.AcceBytesFull = buffer;
                a.Flush();
                a.Close();
                //f.Delete();
            }
            return buffer;
        }

        public void DeleteFile()
        {
            FileInfo f = new FileInfo(this._filepath);

            //byte[] buffer = null;
            if (f.Exists)
            {
                File.Delete(this._filepath);
                //FileStream a = new FileStream(this._filepath, FileMode.Open, FileAccess.Read);
                //buffer = new byte[a.Length];
                //a.Read(buffer, 0, buffer.Length);
                ////memberbinfoacce.AcceBytesFull = buffer;
                //a.Flush();
                //a.Close();
                ////f.Delete();
            }
           
        }

        /// <summary>
        /// 是否是内容头格式:
        /// Content-Disposition: form-data; name=\"file1\"; 
        /// filename=\"C:\\Documents and Settings\\Administrator\\1.jpg
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        private bool IsContentHeader(string name)
        {
            //WebbSystem.TraceMsg(line);
            if ((name == null) || (name == String.Empty))
            {
                return false;
            }
            string[] contentArray = name.Split(';');

            if (contentArray.Length == 3
                && contentArray[0].IndexOf("Content-Type:") >= 0
                && contentArray[1].IndexOf("filename=\"") >= 0
                && contentArray[2].IndexOf("filepath=\"") >= 0)
            {
                return true;
            }
            return false;
        }



        /// <summary>
        /// Save file to disk.
        /// </summary>
        /// <param name="filename"></param>
        public void SaveAs(string filename)
        {
            //主要是了解什么时候保存这人uploadGuid, 这个是进度条用,现在不用了
            string uploadGuid = Utils.GetContext().Request["Sunrise_Web_Upload_UploadGUID"];

            try
            {
                UploadStatus uploadStatus;
                FileInfo fileInfo = new FileInfo(this._filepath);
                //Utils.2writem("SavaAs中filepath信息为:"+ filepath);
                if (uploadGuid != null)
                {
                    uploadStatus = new UploadStatus(uploadGuid);
                    uploadStatus.State = UploadState.Moving;
                    Utils.GetContext().Application[("_UploadGUID_" + uploadGuid)] = uploadStatus;
                }

                string directoryName = Path.GetDirectoryName(filename);
                //Utils.2writem("SavaAs中filename信息为:"+ filename);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                else if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
               

                //Move temporary file to file that client uploaded. Simply change it's name.
                fileInfo.MoveTo(filename);
                if (uploadGuid == null)
                {
                    return;
                }

                uploadStatus = new UploadStatus(uploadGuid);
                uploadStatus.State = UploadState.Completed;

                Utils.GetContext().Application[("_UploadGUID_" + uploadGuid)] = uploadStatus;
            }
            catch (Exception exception)
            {
                if (uploadGuid != null)
                {
                    Utils.GetContext().Application.Remove(("_UploadGUID_" + uploadGuid));
                }

                throw exception;
            }
        }
    }
}

⌨️ 快捷键说明

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