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

📄 imageupload.cs

📁 c#常用类库大全
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Drawing;

/// <summary>
/// 文件类型
/// </summary>
public enum FileExtension
{
    JPG = 255216,
    GIF = 7173,
    BMP = 6677,
    PNG = 13780,
    RAR = 8297,
    jpg = 255216,
    exe = 7790,
    xml = 6063,
    html = 6033,
    aspx = 239187,
    cs = 117115,
    js = 119105,
    txt = 210187,
    sql = 255254
}

/// <summary>
/// 图片检测类
/// </summary>
public static class FileValidation
{
    #region 上传图片检测类
    /// <summary>
    /// 是否允许
    /// </summary>
    public static bool IsAllowedExtension(HttpPostedFile oFile, FileExtension[] fileEx)
    {
        int fileLen = oFile.ContentLength;
        byte[] imgArray = new byte[fileLen];
        oFile.InputStream.Read(imgArray, 0, fileLen);
        MemoryStream ms = new MemoryStream(imgArray);
        System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
        string fileclass = "";
        byte buffer;
        try
        {
            buffer = br.ReadByte();
            fileclass = buffer.ToString();
            buffer = br.ReadByte();
            fileclass += buffer.ToString();
        }
        catch { }
        br.Close();
        ms.Close();
        foreach (FileExtension fe in fileEx)
        {
            if (Int32.Parse(fileclass) == (int)fe) return true;
        }
        return false;
    }

    /// <summary>
    /// 上传前的图片是否可靠
    /// </summary>
    public static bool IsSecureUploadPhoto(HttpPostedFile oFile)
    {
        bool isPhoto = false;
        string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
        string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                isPhoto = true;
                break;
            }
        }
        if (!isPhoto)
        {
            return true;
        }
        FileExtension[] fe = {FileExtension.BMP,FileExtension.GIF,FileExtension.JPG,FileExtension.PNG};

        if (IsAllowedExtension(oFile, fe))
            return true;
        else
            return false;
    }

    /// <summary>
    /// 上传后的图片是否安全
    /// </summary>
    /// <param name="photoFile">物理地址</param>
    public static bool IsSecureUpfilePhoto(string photoFile)
    {
        bool isPhoto = false;
        string Img = "Yes";
        string fileExtension = System.IO.Path.GetExtension(photoFile).ToLower();
        string[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                isPhoto = true;
                break;
            }
        }

        if (!isPhoto)
        {
            return true;
        }
        StreamReader sr = new StreamReader(photoFile, System.Text.Encoding.Default);
        string strContent = sr.ReadToEnd();
        sr.Close();
        string str = "request|<script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
        foreach (string s in str.Split('|'))
        {
            if (strContent.ToLower().IndexOf(s) != -1)
            {
                File.Delete(photoFile);
                Img = "No";
                break;
            }
        }
        return (Img == "Yes");
    }
    #endregion
}

/// <summary>
/// 图片上传类
/// </summary>
//----------------调用-------------------
//imageUpload iu = new imageUpload();
//iu.AddText = "";
//iu.CopyIamgePath = "";
//iu.DrawString_x = ;
//iu.DrawString_y = ;
//iu.DrawStyle = ;
//iu.Font = "";
//iu.FontSize = ;
//iu.FormFile = File1;
//iu.IsCreateImg =;
//iu.IsDraw = ;
//iu.OutFileName = "";
//iu.OutThumbFileName = "";
//iu.SavePath = @"~/image/";
//iu.SaveType = ;
//iu.sHeight  = ;
//iu.sWidth   = ;
//iu.Upload();
//--------------------------------------
public class ImageUpload
{
    #region 私有成员
    private int _Error = 0;//返回上传状态。 
    private int _MaxSize = 1024 * 1024;//最大单个上传文件 (默认)
    private string _FileType = "jpg;gif;bmp;png";//所支持的上传类型用"/"隔开 
    private string _SavePath = System.Web.HttpContext.Current.Server.MapPath(".") + "\\";//保存文件的实际路径 
    private int _SaveType = 0;//上传文件的类型,0代表自动生成文件名 
    private HtmlInputFile _FormFile;//上传控件。 
    private string _InFileName = "";//非自动生成文件名设置。 
    private string _OutFileName = "";//输出文件名。 
    private bool _IsCreateImg = true;//是否生成缩略图。 
    private bool _Iss = false;//是否有缩略图生成.
    private int _Height = 0;//获取上传图片的高度 
    private int _Width = 0;//获取上传图片的宽度 
    private int _sHeight = 120;//设置生成缩略图的高度 
    private int _sWidth = 120;//设置生成缩略图的宽度
    private bool _IsDraw = false;//设置是否加水印
    private int _DrawStyle = 0;//设置加水印的方式0:文字水印模式,1:图片水印模式,2:不加
    private int _DrawString_x = 10;//绘制文本的X坐标(左上角)
    private int _DrawString_y = 10;//绘制文本的Y坐标(左上角)
    private string _AddText = "GlobalNatureCrafts";//设置水印内容
    private string _Font = "宋体";//设置水印字体
    private int _FontSize = 12;//设置水印字大小
    private int _FileSize = 0;//获取已经上传文件的大小
    private string _CopyIamgePath = System.Web.HttpContext.Current.Server.MapPath(".") + "/images/5dm_new.jpg";//图片水印模式下的覆盖图片的实际地址
    #endregion

    #region 公有属性
    /// <summary>
    /// Error返回值
    /// 1、没有上传的文件
    /// 2、类型不允许
    /// 3、大小超限
    /// 4、未知错误
    /// 0、上传成功。 
    /// </summary>
    public int Error
    {
        get { return _Error; }
    }

    /// <summary>
    /// 最大单个上传文件
    /// </summary>
    public int MaxSize
    {
        set { _MaxSize = value; }
    }

    /// <summary>
    /// 所支持的上传类型用";"隔开 
    /// </summary>
    public string FileType
    {
        set { _FileType = value; }
    }

    /// <summary>
    /// 保存文件的实际路径 
    /// </summary>
    public string SavePath
    {
        set { _SavePath = System.Web.HttpContext.Current.Server.MapPath(value); }
        get { return _SavePath; }
    }

    /// <summary>
    /// 上传文件的类型,0代表自动生成文件名
    /// </summary>
    public int SaveType
    {
        set { _SaveType = value; }
    }

    /// <summary>
    /// 上传控件
    /// </summary>
    public HtmlInputFile FormFile
    {
        set { _FormFile = value; }
    }

    /// <summary>
    /// 非自动生成文件名设置。
    /// </summary>
    public string InFileName
    {
        set { _InFileName = value; }
    }

    /// <summary>
    /// 输出文件名
    /// </summary>
    public string OutFileName
    {
        get { return _OutFileName; }
        set { _OutFileName = value; }
    }

    /// <summary>
    /// 输出的缩略图文件名
    /// </summary>
    public string OutThumbFileName
    {
        get;
        set;
    }

    /// <summary>
    /// 是否有缩略图生成.
    /// </summary>
    public bool Iss
    {
        get { return _Iss; }
    }

    /// <summary>
    /// 获取上传图片的宽度
    /// </summary>
    public int Width
    {
        get { return _Width; }
    }

    /// <summary>
    /// 获取上传图片的高度
    /// </summary>
    public int Height

⌨️ 快捷键说明

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