一、 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法。

Boolean fileOk = false;
string path = Server.MapPath("~/images/");
//判断是否已经选取文件
if (FileUpload1.HasFile)
{
//取得文件的扩展名,并转换成小写
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
//限定只能上传jpg和gif图片
string[] allowExtension = { ".jpg", ".gif" };
//对上传的文件的类型进行一个个匹对
int j = ;
for (int i = ; i < allowExtension.Length; i++)
{
if (fileExtension == allowExtension[i])
{
fileOk = true;
return;
}
else
{
j++;
}
}
if (j > )
{
Response.Write("<script>alert('文件格式不正确');</script>");
return;
}
}
else
{
Response.Write("<script>alert('你还没有选择文件');</script>");
return;
}
//如果扩展名符合条件,则上传
if (fileOk)
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
   二、不检测文件后缀而是检测文件MIME内容类型。 Boolean fileOk = false;
string path = Server.MapPath("~/images/");
//判断是否已经选取文件
if (FileUpload1.HasFile)
{
//取得文件MIME内容类型
string type = this.FileUpload1.PostedFile.ContentType.ToLower();
if (type.Contains("image")) //图片的MIME类型为"image/xxx",这里只判断是否图片。
{
fileOk = true; }
else
{
Response.Write("<script>alert('格式不正确')</script>");
}
}
else
{
Response.Write("<script>alert('你还没有选择文件');</script>");
}
//如果扩展名符合条件,则上传
if (fileOk)
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
   三、可以实现真正意义上的文件类型判断 try
{
//判断是否已经选取文件
if (FileUpload1.HasFile)
{
if (IsAllowedExtension(FileUpload1))
{
string path = Server.MapPath("~/images/");
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
else
{
Response.Write("<script>alert('您只能上传jpg或者gif图片');</script>");
} }
else
{
Response.Write("<script>alert('你还没有选择文件');</script>");
}
}
catch (Exception error)
{
Response.Write(error.ToString());
}
#endregion
}
//真正判断文件类型的关键函数
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.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
{ }
r.Close();
fs.Close();
if (fileclass == "" || fileclass == "")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
{
return true;
}
else
{
return false;
} }

源自:http://www.cnblogs.com/shouce/p/5084567.html

ASP.net 判断上传文件类型的三种方法的更多相关文章

  1. 用ASP.net判断上传文件类型的三种方法

    一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false;           ...

  2. JS判断上传文件类型

    /*   * 判断图片类型   */    function checkImgType(ths){        if (ths.value == "") {            ...

  3. Python判断上传文件类型

    在开发上传服务时,经常需要对上传的文件进行过滤. 本文为大家提供了python通过文件头判断文件类型的方法,非常实用. 代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  4. JS 判断上传文件类型

    var video_src_file = $("#video_src_file").val(); var fileTypes = new Array("flv" ...

  5. ASP.NET FileUpload 上传文件类型验证

    验证的核心方法: public static bool IsAllowedExtension(FileUpload hifile) { //原方法是这样的,会提示找不到文件 //System.IO.F ...

  6. python 如何找到某一目录下的文件类型(三种方法)

    #!/usr/bin/env python import glob import os os.chdir(“./”) for file in glob.glob(“*.py”): print file ...

  7. HTML5中的input type为file控件限制上传文件类型及扩展

    简单介绍 input file控件限制上传文件类型如下:1.文件类型中间用,分开:2.html和htm这样的要写成两个: 3实例: <input type="file" na ...

  8. Struts2框架下的文件上传文件类型、名称约定

    Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...

  9. input file标签限制上传文件类型

    用 input 的file类型标签上传文件,有时需要限制上传文件类型,添加accept属性可以实现 <input type="file" accept="image ...

随机推荐

  1. Objective-C 【@property和@synthesize关键字】

    ------------------------------------------- @property关键字的使用及注意事项 直接上代码和注释了! // //@property关键字的使用 //① ...

  2. SQL之存储过程,仿数组

    create procedure update_ERPTreeList(@s1 varchar(),@s2 varchar()) As Begin declare @ss1 varchar(),@ss ...

  3. linux FTP 批量下载文件

    wget是一个从网络上自动下载文件的自由工具,支持通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理.wget名称的由来是“World Wide Web”与“ge ...

  4. L003-oldboy-mysql-dba-lesson03

          L003-oldboy-mysql-dba-lesson03 IOPS:每秒钟输入输出的数量 [root@web01 ~]# cat /proc/meminfo [root@web01 ~ ...

  5. Android SDK Manager国内无法更新的解决方案

    万里长城永不倒,千里黄河水滔滔.算了跑题了. 但还是要吐槽这下这个万里长城,感谢 方滨兴 叫兽 给我们净化了互联网,靠!什么&!@#¥ 此处略去一万字. 现在由于GWF,google基本和咱们 ...

  6. php中类的声明与使用

    <?php /**php语言是支持面向对象编程的,对于面向对象的编程,学过java和C++的人都知道啊! *如果不清楚的去baidu问一下就可以了. */ //我们来定义一个类,定义类的关键字是 ...

  7. 【刷机】Google Nexus s 蓝牙点击异常,无法启动,刷机解决方案

    1  问题详述 手头上有一部Google Nexus S ,本机自带的输入法不好用,想下载其他的输入法,想用蓝牙传输一下apk文件,点了一下蓝牙开关想要打开蓝牙功能,但奇怪的情况出现了,手机一直重启, ...

  8. 正则表达式模块re

    正则表达式通过特定的式子匹配某种类型的字符串.对字符串进行匹配验证或者从大量的字符串里提取出特定的信息. 正则表达式包含普通字符和特殊字符.大部分的普通字符如‘A’或‘0’可以简单的来匹配自己.如te ...

  9. Beaglebone Back学习三(开发环境搭建)

    开发环境搭建 1 Ubuntu环境搭建 2 Window环境搭建 3 开发板环境搭建 1 Ubuntu环境搭建 (1)安装必要的网络工具 samba nfs tftp vmware-tools sam ...

  10. Could not retrieve mirrorlist http://mirrorlist.centos.org || PYCURL ERROR 6

    yum:Could not retrieve mirrorlist http://mirrorlist.centos.org || PYCURL ERROR 6 通过centos安装openldap的 ...