惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
B
Blog
P
Proofpoint News Feed
美团技术团队
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
有赞技术团队
有赞技术团队
小众软件
小众软件
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
罗磊的独立博客
L
LangChain Blog
GbyAI
GbyAI
腾讯CDC
T
The Blog of Author Tim Ferriss
Microsoft Security Blog
Microsoft Security Blog

博客园 - Sphix

根据银行卡获取银行卡开户银行和类型 HTTP could not register URL http://+:8000/testservice/. Your... 一些必不可少的Sublime Text 2插件 C# CultureInfo列表 解决"There is already an open DataReader associated with this Command which must be closed first." exception in EF 中 C#正则表达式整理备忘 Lucene.net 开发记录 在Asp.net 4.0 中动态注册HttpModule ASP.net 视频上传转换flv并且抓取第一帧生成图片源码 揭秘全球最大网站Facebook背后的那些软件 asp.net网站管理工具 的 地址(Web Site Administration Tool ) Sql Server 2008 Full-text search Error: Word breaking timed out for the full-text query string. Associations in EF Code First CTP5: Part 1 – Complex Types C# Enum 类型的本地化 wordpress 文章缩略图功能 简单实际的方式分隔Admin 区域 EF4 中Self-track entity 错误用于单web开发中要注意的地方 Asp.net文件下载 SQLite 资源汇总
C#验证文件类型
Sphix · 2011-02-11 · via 博客园 - Sphix

有的时候需要检测上传文件的真实类型,才能准确的判断用户上传的文件是否真的是需要过滤的文件类型
大多数情况下我们都是用 Path.GetExtension(file.FileName)  获取文件的扩展名,然后进行判断文件是否是我们需要过滤的文件,但是这种方法只能得到表面上的扩展名,如果一些恶作剧的用户故意把 text的文件更改为 jpg 那么Path.GetExtension(file.FileName) 获取到的文件类型就是 jpg 而不是text
用下面的方法会得到文件的真实类型
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;       
        }