上传文件检测类型的三种方法:
1.检测文件的后缀名
2.检测文件的头部编码,不同类型文件的头部编码不一样。
3.检测文件中的MIME内容类型。
前台文件三种都一样:
<%@ Page Language="C#"
AutoEventWireup=
"true"
CodeFile=
"Default.aspx.cs"
Inherits=
"_Default"
%><BR>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"<A href="
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22%3E
" target=_blank>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
></A><BR>
<html xmlns=
"<A href="
http://www.w3.org/1999/xhtml%22%C2%A0%3E
" target=_blank>http://www.w3.org/1999/xhtml"
></A><BR><head runat=
"server"
><BR>
<title>无标题页</title><BR>
</head>
<BR>
<body>
<BR>
<form id=
"form1"
runat=
"server"
><BR>
<div><BR>
<asp:FileUpload ID=
"FileUpload1"
runat=
"server"
/><BR>
<asp:Button ID=
"btn_upload"
runat=
"server"
<SPAN>OnClick</SPAN>=
"btn_upload_Click"
Text=
"传"
/><BR>
</div><BR>
</form><BR>
</body><BR>
</html>
后台文件:
第一种方法:
安全性相对第二种方法低,把文本文件1.txt改称1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法。
public partial class _Default:System.Web.UI.Page
{
protected void btn_upload_Click(object sender,EventArgs e)
{
Boolean fileOK=false;
string path=Server.MapPath(
"~/images/"
);
if(FileUpload1.HasFile)
{
string fileExtension=System.IO.Path.GetExtension=(FileUpload1.FileName).ToLower();
string[] allowExtension={
".jpg",".gif"};
for(int i=0;i<allowExtension.Length;i++)
{
if(fileExtension==allowExtension[i])
{
fileOK=true;
break;
}
}
}
else
{
Response.Write("<script> alert('你还没有选择文件');</script>");
}
if(fileOK)
{
FileUpload1.PostedFile.SaveAs(path+FileUpload1.FileName);
Response.Write("<script>alert('文件上传成功');</script>");
}
}
}
第二种方法,可实现真正意义上的文件类型判断。
public partial class _Default:System.Web.UI.Page
{
protected void btn_upload_Click(object sender,EventArg e)
{
try
{
if(FileUpload1.HasFile)
{
if(IsAllowedExtension(FileUpload1))
{
string panth=Servert.MapPath(
"~/images/"
);
FileUpload1.PostedFile.SaveAs(path+FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
else
{
Response.Write("<script>alert('您只能上传jpg或者gif图片');</script>");
}
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
//真正判断文件类型的关键函数
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")
{
return true;
}
else
{
return false;
}
}
}
缺点是FileStream只能访问本地文件,不能访问远程文件。
第三种方法是第一种方法的改进,把文本文件1.txt改成1.jpg不能上传,不检测文件后缀名,而是检测文件MIME内容类型。
public partial class _Default:System.Web.UI.Page
{
protected void bth_upload_Click(object sender,EventArgs e)
{
Boolean fileOK=false;
string path=Server.MapPath(
"~/images/"
);
if(FileUpload1.HasFile)
{
string type=this.uploadfile.PostedFile.ContentType.ToLower();
if(type.Contains("image"))
{
fileOK=true;
}
}
else
{
Response.Write("<script>alert('你还没有选择文件');</script>");
}
if(fileOK)
{
FileUpload1.PostedFile.SaveAs(path+FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
}
}
总结:第一种方法虽然简单,但安全性不够;第二种方法虽然安全性没问题,但fileStream不能访问远程文件;方法三与方法二的安全性相当,而且实现也简单,建议使用。