zoukankan      html  css  js  c++  java
  • 判断上传的图片文件格式是否合法不是用后缀做的判断

    /// <summary>
        /// 检查文件格式是否充许
        /// </summary>
        /// <param name="ImageFormat">待检查的文件格式</param>
        /// <returns>是否合法</returns>
        private bool HasThisForamt(string ImageFormat)
        {
            string ImageFormats = "gif|png|gif|bmp";
            if (ImageFormats.ToLower().IndexOf(ImageFormat.ToLower())>=0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断上传的图片文件格式是否合法
        /// </summary>
        /// <param name="upControl">文件上传控件</param>
        /// <returns>是否合法</returns>
        private bool IsImageFormat( FileUpload upControl )
        {
            try
            {
                using (System.Drawing.Image img = System.Drawing.Image.FromStream(upControl.FileContent))
                {
                    if (HasThisForamt("Bmp") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) return true;
                    if (HasThisForamt("Emf") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf)) return true;
                    if (HasThisForamt("Exif") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif)) return true;
                    if (HasThisForamt("Gif") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) return true;
                    if (HasThisForamt("Icon") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon)) return true;
                    if (HasThisForamt("Jpeg") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) return true;
                    if (HasThisForamt("MemoryBmp") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp)) return true;
                    if (HasThisForamt("Png") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png)) return true;
                    if (HasThisForamt("Tiff") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff)) return true;
                    if (HasThisForamt("Wmf") && img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf)) return true;
                }
            }
            catch
            {
                //此文件不是圖像文件
            }
            //您所選擇的文件格式不充许
            return false;
        }


    //...........

        public bool IsEligibilityFileFormat(FileUpload fs)
        {
            bool xx = false;
            //System.IO.FileStream fs = new System.IO.FileStream(sFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs.FileContent);
            string bx = " ";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                bx = buffer.ToString();
                buffer = r.ReadByte();
                bx += buffer.ToString();
            }
            catch (Exception exc)
            {
            }
            r.Close();
            //fs.Close();
            Response.Write(bx);
            if (bx == "7790 " || bx == "8297 " || bx == "8075 ")     //7790:exe,8297:rar,8075:pk  
            {
                xx = true;
            }
            return xx;
        /*
        7173 gif
        255216 jpg
        7790 exe dll
        00 ani--ico--cur
        7783
        255254 --ini
        9146 -- ini
        5866
        6395 hlp
        8269 reg
        70105 log
        205168
        7384 chm
        5549 txt
        117115 txt
        5450 txt
        5666 psd
        255254 rdp
        10056 bt种子
        8297 rar
        64101 bat
        */
        }

     

  • 相关阅读:
    MySQL优化
    右键菜单怎样添加“在此处打开命令提示符”选项
    MemSQL与MySQL不兼容问题总结
    Ubuntu Server 18.04 修改网路配置
    How to Install MemSQL
    Metro UI 菜单(Winform)
    Windows Server 2008 系统设置集合
    推荐个好东西swoole,php如虎添翼
    php的swoole扩展中onclose和onconnect接口不被调用的问题
    关于编程语言(转/收藏)-原文作者:韩天峰(Rango)
  • 原文地址:https://www.cnblogs.com/0000/p/1600972.html
Copyright © 2011-2022 走看看