发布于 2016-06-10 05:14:06 | 147 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


这篇文章主要介绍了用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>"); 
} 

二、不检测文件后缀而是检测文件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 == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务