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

📄 fileuploader.cs

📁 这是一个简单的论坛程序源码
💻 CS
字号:
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NetFocus.Web.Core;

namespace NetFocus.Web.Applications.Forum 
{
    /// <summary>
    /// 用来上传文件
    /// 方法:Save()保存文件到指定路径
    /// 属性:Path=保存到的路径,FileType=允许的文件类型,“|”分隔,Sizes=允许的文件大小,KB单位,CreatDirectory=默认建立新的下级目录,NeedWaterMark =默认加水印
    /// </summary>
    public class FileUploader
    {
        private string path = null;
        private string fileType = null;
        private string errorMessage = "";//"0"=上传失败
        private int sizes = 0;
        bool creatDirectory;
        bool needWaterMark;
        /// 
        /// 初始化变量
        /// 
        public FileUploader()
        {
            path = "/UploadFiles/Image"; //上传路径
            fileType = "jpg|gif|bmp|JPG|GIF|JPEG|Jpeg";
            sizes = 1024 * 1024; //传文件的大小,默认1024KB
            creatDirectory = true;//默认建立新的下级目录
            needWaterMark = true;//默认加水印
        }

        /// 
        /// 设置上传路径,如:UploadFiles\
        /// 
        public string Path
        {
            set
            {
                path = value;
            }
        }

        /// 
        /// 设置上传文件大小,单位为KB
        /// 
        public int Sizes
        {
            set
            {
                sizes = value * 1024;
            }
        }

        /// 
        /// 设置上传文件的类型,如:jpg|gif|bmp /// 
        public string FileType
        {
            set
            {
                fileType = value;
            }
        }

        public bool CreatDirectory
        {
            set
            {
                creatDirectory = value;
            }
        }

        public bool NeedWaterMark
        {
            set
            {
                needWaterMark = value;
            }
        }

        /// 
        /// 上传图片
        /// 返回上传图片的相对路径
        /// 
        public string Save(System.Web.UI.WebControls.FileUpload name)
        {
            //try
            //{
            string filePath = null;
            //以当前时间修改图片的名字或创建文件夹的名字
            string modifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            //获得站点的物理路径
            string uploadFilePath = null;
            string forWaterMarkPath = null;
            //如果为true则以当前年月创建文件夹,否则为设置的文件夹
            if (creatDirectory)
            {
                uploadFilePath = WebContext.Current.HttpContext.Server.MapPath(Globals.ApplicationPath + path + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString()) + @"\";
                forWaterMarkPath = Globals.ApplicationPath + path + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + @"/";
            }
            else
            {
                uploadFilePath = WebContext.Current.HttpContext.Server.MapPath(Globals.ApplicationPath + path) + @"\";
                forWaterMarkPath = Globals.ApplicationPath + path + @"/";
            }
            //获得文件的上传的路径
            string sourcePath = name.FileName.ToString().Trim();
            //判断上传文件是否为空
            if (!name.HasFile)
            {
                errorMessage = "0";
                return errorMessage;
            }
            //获得文件扩展名
            string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
            //获得上传文件的大小
            long strLen = name.PostedFile.ContentLength;
            //分解允许上传文件的格式
            string[] temp = fileType.Split('|');
            //设置上传的文件是否是允许的格式
            bool flag = false;
            //判断上传文件大小
            if (strLen >= sizes)
            {
                errorMessage = "0";
                //errorMessage = "上传的图片不能大于" + sizes/1024 + "KB";
                return errorMessage;
            }
            //判断上传的文件是否是允许的格式
            foreach (string data in temp)
            {
                if (data.ToLower() == tFileType.ToLower())
                {
                    flag = true;
                    break;
                }
            }
            //如果为真允许上传,为假则不允许上传
            if (!flag)
            {
                errorMessage = "0";
                //errorMessage = "文件格式不符合要求,合法的文件格式为:" + fileType;
                return errorMessage;
            }
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadFilePath);
            //判断文件夹否存在,不存在则创建
            if (!dir.Exists)
            {
                dir.Create();
            }
            if (creatDirectory)
            {
                filePath = uploadFilePath + modifyFileName + "." + tFileType;
            }
            else
            {
                filePath = System.IO.Path.Combine(uploadFilePath, modifyFileName + "." + tFileType);
            }

            name.PostedFile.SaveAs(filePath);

            //加水印
            forWaterMarkPath = forWaterMarkPath + modifyFileName + "." + tFileType;
            //try
            //{
            //if (needWaterMark)
            //{
            //    WaterMark getLogo = new WaterMark();
            //    getLogo.TypeOfMark = "text";
            //    getLogo.imgFilePath = forWaterMarkPath;
            //    getLogo.StringOfMark = ConfigurationManager.AppSettings["StringOfMark"];
            //    getLogo.CreatWaterMark();
            //}
            //}
            //catch
            //{
            //    Exception e = new Exception("水印添加失败,忽略该错误");
            //    throw e;
            //}
            return forWaterMarkPath;

            //}
            //catch
            //{
            //    //异常
            //    errorMessage = "0";
            //    throw new Exception("出现未知错误,请检查目录权限!");
            //    return errorMessage;
            //}
        }

    }
}

⌨️ 快捷键说明

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