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

📄 filemanger.cs

📁 我的一个Ado.Net一个框架设计
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;


namespace CommonUtility
{
    public class FileManger
    { 
        /// <summary>
        /// 上传图片类型文件,没有经过加工
        /// </summary>
        /// <param name="file">一个用于上传的控件</param>
        /// <param name="upDummyPath">文件要上传服务器上的虚拟路径</param>
        /// <returns></returns>
        public string UpOriginalImg(HtmlInputFile file, string upDummyPath)
        {
            string fileName = UpImageFile(file, upDummyPath);
            fileName = fileName.Substring(fileName.LastIndexOf("/")+1);
            return fileName;
        }
        /// <summary>
        ///上传需要加工的图片
        /// </summary>
        /// <param name="file">上传控件</param>
        /// <param name="upDummyPath">上传服务器上的虚拟路径</param>
        /// <param name="size">上传文件需要处理后的大小,即文件的宽和高</param>
        /// <param name="bgColor">文件的北京色</param>
        /// <returns></returns>
        public string UpAlterableImg(HtmlInputFile file, string upDummyPath, Size size, string bgColor)
        {
            string fullFilePath = UpImageFile(file, upDummyPath);
            string saveFileName = upDummyPath + "/A" + fullFilePath.Substring(fullFilePath.LastIndexOf("/O")+2);
            string deleteOriginalFile = upDummyPath + fullFilePath.Substring(fullFilePath.LastIndexOf("/O"));
            string onlyName = saveFileName.Substring(saveFileName.LastIndexOf("/A")+1);            
            saveFileName = HttpContext.Current.Server.MapPath(saveFileName);
            Image operateImage = Image.FromFile(fullFilePath);
            int imgWidth = size.Width;
            int imgHeight = size.Height;
            if (size.Width != 320 && size.Height != 302)
            {
                if (operateImage.Width > imgWidth || operateImage.Height > imgHeight)
                {
                    if (operateImage.Height / imgHeight > operateImage.Width / imgWidth)
                    {
                        imgWidth = operateImage.Width * imgHeight / operateImage.Height;
                    }
                    else
                    {
                        imgHeight = operateImage.Height * imgWidth / operateImage.Width;
                    }
                }
            }
            //构造一个指定宽高的Bitmap 
            Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
            Graphics graphics = Graphics.FromImage(bitmap);
            Color color;
            if (bgColor == null)
                color = Color.FromName("white");
            else
                color = Color.FromName(bgColor);
            //用指定的颜色填充Bitmap 
            graphics.Clear(color);
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawImage(operateImage, new Rectangle(0, 0, imgWidth, imgHeight), new Rectangle(0, 0, operateImage.Width, operateImage.Height), GraphicsUnit.Pixel);
            try
            {
                //D:\\ProjectFile\\DemingHome\\Web\\Images\\PictureAppreciate\\0761211574289yuanyuan.jpg
                bitmap.Save(saveFileName, System.Drawing.Imaging.ImageFormat.Jpeg);                
                graphics.Dispose();
                bitmap.Dispose();
                operateImage.Dispose();
                DeleteFile(deleteOriginalFile);//删除源文件
                return onlyName ;
            }
            catch
            {
                throw;
            }            
        }
        public string UpImageFile(HtmlInputFile file, string upDummyPath)
        {
            string upRealPath = "";
            if (upDummyPath.Trim().Length > 0)//判断文件路径是否为空
            {
                upRealPath = HttpContext.Current.Server.MapPath(upDummyPath);//返回与web服务器上的指定虚拟的相对路径转化为相对应的物理路径
                bool isExist = System.IO.Directory.Exists(upRealPath);//判断路径是否存在
                if (!isExist)//如不存在该路径,那么创建该路径
                {
                    Directory.CreateDirectory(upRealPath);
                }
            }
            if (file.PostedFile.ContentLength > 1048576)
            {
                JScript.MegBox("对不起,文件太大,你只能上传1M以下的图片,请重试!");
            }
            string clientFilePath = file.PostedFile.FileName;//获取客户端文件的物理路径
            string fileExtendName = clientFilePath.Substring(clientFilePath.LastIndexOf(".") + 1);//取文件扩展名
            //限制上传文件的类型,只有以下类型文件才可以上传
            if (fileExtendName.ToLower() != "jpg" && fileExtendName.ToLower() != "gif" && fileExtendName.ToLower() != "bmp" && fileExtendName.ToLower() != "jpeg")
            {
                HttpContext.Current.Response.Write("<script>alert('对不起,请选择你要上传的图片文件!');</script>");
                return "OnlyImageType";
            }
            //获取文件名字,如:0760616332692yuanyuan.jpg
            string fileName = string.Format("{0:yyMddHHmmssff}", DateTime.Now) + clientFilePath.Substring(clientFilePath.LastIndexOf("\\") + 1);
            string fullFilePath = upRealPath + "/O" + fileName;
            try
            {
                file.PostedFile.SaveAs(fullFilePath);//上传文件
                return fullFilePath;
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 给出指定的虚拟路径,进行删除
        /// </summary>
        /// <param name="fileDummyPath"></param>
        /// <returns></returns>
        public bool DeleteFile(string fileDummyPath)
        {
            string fileRealPath = HttpContext.Current.Server.MapPath(fileDummyPath);
            bool isExist=System.IO.File.Exists(fileRealPath);
            if (isExist)
            {
                try
                {
                    System.IO.File.Delete(fileRealPath);
                    return true;
                }
                catch
                {
                    throw;
                }
            }
            else
            {
                return false;
            }
        }
        public string UpAnyFile(HtmlInputFile file, string upDummyPath)
        {
            string upRealPath = "";
            if (upDummyPath.Trim().Length > 0)//判断文件路径是否为空
            {
                upRealPath = HttpContext.Current.Server.MapPath(upDummyPath);//返回与web服务器上的指定虚拟的相对路径转化为相对应的物理路径
                bool isExist = System.IO.Directory.Exists(upRealPath);//判断路径是否存在
                if (!isExist)//如不存在该路径,那么创建该路径
                {
                    Directory.CreateDirectory(upRealPath);
                }
            }
            if (file.PostedFile.ContentLength > 20048576)
            {
                JScript.MegBox("对不起,文件太大,你只能上传20M以下的文件,请重试!");
            }
            string clientFilePath = file.PostedFile.FileName;//获取客户端文件的物理路径
            string fileExtendName = clientFilePath.Substring(clientFilePath.LastIndexOf(".") + 1);//取文件扩展名           
            //获取文件名字,如:0760616332692yuanyuan.jpg
            string fileName = string.Format("{0:yyMddHHmmssff}", DateTime.Now) + clientFilePath.Substring(clientFilePath.LastIndexOf("\\") + 1);
            string fullFilePath = upRealPath + "/" + fileName;
            try
            {
                file.PostedFile.SaveAs(fullFilePath);//上传文件
                return fileName;
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 写入文件,根据摸板,生成静态页面
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="author">作者</param>
        /// <returns></returns>
        public string WriteIntoFile(string title, string content, string author)
        {
            string path = HttpContext.Current.Server.MapPath("../StaticPage/StaticHtml/");
            Encoding code = Encoding.GetEncoding("gb2312");
            // 读取模板文件 
            string temp = HttpContext.Current.Server.MapPath("../StaticPage/HtmlTemplate.htm");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }
            string htmlCode = DateTime.Now.ToString("yyyyMMddHHmmss") + author + ".html";
            // 替换内容 
            // 这时,模板文件已经读入到名称为str的变量中了 
            str = str.Replace("ShowArticle", title); //模板页中的ShowArticle 
            str = str.Replace("biaoti", title);
            str = str.Replace("content", content);
            str = str.Replace("author", author);
            // 写文件 
            try
            {
                sw = new StreamWriter(path + htmlCode, false, code);
                sw.Write(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
            return "/StaticPage/StaticHtml/" + htmlCode;
        }
        /// <summary>
        /// 从文件读取
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string ReadFromFile(string filePath)
        {
            System.IO.StreamReader Sr = new System.IO.StreamReader(System.Web.HttpContext.Current.Server.MapPath(filePath), System.Text.Encoding.Default);//.GetEncoding("GB2312"));
            string fileContent = HttpContext.Current.Server.HtmlEncode(Sr.ReadToEnd()).ToString();
            int start = fileContent.IndexOf("id=&quot;Content&quot;&gt;", 0) + "id=&quot;Content&quot;&gt;".Length;
            int end = fileContent.IndexOf("id=&quot;CreatePerson", 0) - 41;
            int length = end - start;
            fileContent = fileContent.Substring(start, length);
            Sr.Close();
            return fileContent;
        }
    }
}

⌨️ 快捷键说明

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