zoukankan      html  css  js  c++  java
  • C# 检测真实的文件类型函数

    Code
     private bool IsAllowedExtension(HttpPostedFile hifile)
            {
                
    bool ret = false;

                System.IO.FileStream fs 
    = new System.IO.FileStream(hifile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader r 
    = new System.IO.BinaryReader(fs);
                
    string fileclass = "";
                
    byte buffer;
                
    try
                {
                    buffer 
    = r.ReadByte();
                    fileclass 
    = buffer.ToString();
                    buffer 
    = r.ReadByte();
                    fileclass 
    += buffer.ToString();
                }
                
    catch
                {
                    
    return false;
                }
                r.Close();
                fs.Close();
                
    /*文件扩展名说明
                 *7173        gif 
                 *255216      jpg
                 *13780       png
                 *6677        bmp
                 *239187      txt,aspx,asp,sql
                 *208207      xls.doc.ppt
                 *6063        xml
                 *6033        htm,html
                 *4742        js
                 *8075        xlsx,zip,pptx,mmap,zip
                 *8297        rar   
                 *01          accdb,mdb
                 *7790        exe,dll           
                 *5666        psd 
                 *255254      rdp 
                 *10056       bt种子 
                 *64101       bat 
                 
    */


                String[] fileType 
    = { "255216""7173""6677""13780""8297""5549""870""87111""8075" };

                
    for (int i = 0; i < fileType.Length; i++)
                {
                    
    if (fileclass == fileType[i])
                    {
                        ret 
    = true;
                        
    break;
                    }
                }
                
    return ret;       
            }
  • 相关阅读:
    堆栈,堆栈,堆和栈的区别(转贴)
    .net cookie跨域和逗号bug的修复
    转:互联网协议入门
    函数式编程stream.js
    用例子验证w3c的stacklevel在不同浏览器中的显示
    JS创建对象的几种方法
    打开Word时总是出现 “Microsoft Office Word 需要 VBA 宏语言支持来完成此操作
    如何解决闭包只能取得包含函数中任何变量的最后一个值
    转:10个javascript简写/优化技巧
    Nicholas C. Zakas如何面试前端工程师
  • 原文地址:https://www.cnblogs.com/nick4/p/1373190.html
Copyright © 2011-2022 走看看