zoukankan      html  css  js  c++  java
  • 根据文件名判断文件扩展名 和 根据流真正判断文件类型的关键函数

    /// <summary>
       /// 根据文件名判断文件扩展名 和 根据流真正判断文件类型的关键函数
       /// </summary>
       /// <param name="thefile">上传的文件</param>
       /// <returns>true - 允许上传的文件类型 false-不允许</returns>
       public static bool CheckUploadImgExtension(System.Web.HttpPostedFile thefile)
       {
        //取得文件的扩展名,并转换成小写
        string fileExtension = System.IO.Path.GetExtension( thefile.FileName).ToLower();

        //限定只能上传jpg和gif图片
        string[] allowExtension = { ".jpg", ".gif", ".bmp" };

        bool fileOk = false;
        //对上传的文件的类型进行一个个匹对
        for (int i = 0; i < allowExtension.Length; i++)
        {
         if (fileExtension == allowExtension[i])
         {
          fileOk = true;

          System.IO.BinaryReader reader = new System.IO.BinaryReader( thefile.InputStream );
          string fileclass = "";
          byte buffer;
          try
          {
           buffer = reader.ReadByte();
           fileclass = buffer.ToString();
           buffer = reader.ReadByte();
           fileclass += buffer.ToString();
          }
          catch
          {
           fileOk = false;
          }
          reader.Close();

          //说明 255216 是jpg; 7173 是gif; 6677 是BMP, 13780是PNG; 7790 是exe, 8297 是rar
          if (fileclass == "255216" || fileclass == "7173" || fileclass == "6677")
          {
           fileOk = true;
          }
          else
          {
           fileOk = false;
          }

          break;
         }
        }

        return fileOk;
       }

  • 相关阅读:
    VS2010 MFC对话框程序用CButtonST给按钮添加图标
    VS2010 MFC 使用GDI+给图片添加汉字
    C++ Primer(第4版)-学习笔记-第2部分:容器和算法
    C++ 面向对象编程
    C++类(Class)总结
    delegate、notification、KVO场景差别
    iOS block种类和切换
    Copy 与MutableCopy的区别
    ios 避免循环引用
    WKInterfaceImage 无法更新图片的问题
  • 原文地址:https://www.cnblogs.com/appleseed/p/1247828.html
Copyright © 2011-2022 走看看