用ASP.net判断上传文件类型的三种方法
一、 安全性比较低,把文本文件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 = 0; for (int i = 0; i < allowExtension.Length; i++) { if (fileExtension == allowExtension[i]) { fileOk = true; return; } else { j++; } } if (j > 0) { 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>"); }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 == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar { return true; } else { return false; } }用ASP.net判断上传文件类型的三种方法的更多相关文章
- ASP.net 判断上传文件类型的三种方法
一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false; string pa ...
- JS判断上传文件类型
/* * 判断图片类型 */ function checkImgType(ths){ if (ths.value == "") { ...
- Python判断上传文件类型
在开发上传服务时,经常需要对上传的文件进行过滤. 本文为大家提供了python通过文件头判断文件类型的方法,非常实用. 代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- JS 判断上传文件类型
var video_src_file = $("#video_src_file").val(); var fileTypes = new Array("flv" ...
- ASP.NET FileUpload 上传文件类型验证
验证的核心方法: public static bool IsAllowedExtension(FileUpload hifile) { //原方法是这样的,会提示找不到文件 //System.IO.F ...
- python 如何找到某一目录下的文件类型(三种方法)
#!/usr/bin/env python import glob import os os.chdir(“./”) for file in glob.glob(“*.py”): print file ...
- HTML5中的input type为file控件限制上传文件类型及扩展
简单介绍 input file控件限制上传文件类型如下:1.文件类型中间用,分开:2.html和htm这样的要写成两个: 3实例: <input type="file" na ...
- Struts2框架下的文件上传文件类型、名称约定
Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...
- input file标签限制上传文件类型
用 input 的file类型标签上传文件,有时需要限制上传文件类型,添加accept属性可以实现 <input type="file" accept="image ...
随机推荐
- Vessels
Codeforces Round #218 (Div. 2) D:http://codeforces.com/problemset/problem/371/D 题意:就是有一些盘子,盘子里可以装水,这 ...
- Replicate String in C#
My original posting on string repetition caused a couple responses, and is currently among the Top P ...
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
- Cash Machine(多重背包)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24067 Accepted: 8414 Description A Ba ...
- page-object使用(1)
创建你的page 你必须做的第一件事情是创建你的page,这是一些包含了PageObject模块的简单的ruby类,请不要创建你自己的initialize方法,因为已经有一个存在而且不能被覆盖.如果你 ...
- HDOJ(HDU) 2304 Electrical Outlets(求和、、)
Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is ...
- Java系统变量设置方式
近期碰到一个编码的问题,发现整个平台都是用的GB2312,因此导致webservice调用时有些字不能正常接受. 反编译中间件的源码如下: public static final String nod ...
- OpenWrt简要刷机教程
准备工作 1. 下载openwrt中文固件到PC.(当然其他英文固件也可) 2 找到路由器的RST键. 3 找到路由器刷机口---姑且称之为“WAN口” 4. 关闭路由器的电源. 5. 将PC网口 ...
- CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案
题意 n个数1~n按顺序围成一个圈...现在在某些两点间加边..边可以加在圈内或者圈外..问是否会发生冲突?如果不发生冲突..输每一条边是放圈内还是圈外. 题解 ...
- scipy安装失败
pip install scipy安装失败 可以从uci网站下载wheel安装包然后执行pip install xx.whl进行安装 http://www.lfd.uci.edu/~gohlke/py ...