当前位置: 移动技术网 > IT编程>开发语言>c# > C#检测上传文件真正类型的方法

C#检测上传文件真正类型的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#检测上传文件真正类型的方法。分享给大家供大家参考。具体分析如下:

对于用户上传的文件如果只是根据扩展名判断,很容易上传上来可执行文件,这是非常危险的,这段代码可以在服务器端检测上传文件的真实类型。

<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" 
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
 void alert(string s)
 {
  page.clientscript.registerstartupscript(page.gettype(), "js", "alert('" + s + "')", true);
 }
 protected void button1_click(object sender, eventargs e)
 {
  savefile();
 }
 protected string savefile()
 {
  string maxsize = "1024";
  //最大文件大小
  int imgmaxsize = convert.toint32(maxsize) * 1024 * 1024;
  httppostedfile imgfile = fuimg.postedfile;
  if (imgfile == null || fuimg.filename == "")
  {
   alert("请选择文件。");
   return "";
  }
  string dirpath = server.mappath("~/");
  string saveurl = page.resolveurl("~/");
  if (!system.io.directory.exists(dirpath))
  {
   alert("上传目录不存在。");
   return "";
  }
  string filename = imgfile.filename;
  string fileext = system.io.path.getextension(filename).tolower();
  if (imgfile.inputstream == null || imgfile.inputstream.length > imgmaxsize)
  {
   alert("上传文件大小超过限制。");
   return "";
  }
  //验证文件格式
  string fpath = isallowedextension(imgfile);
  if ("" == fpath)
  {
   alert("图片格式不正确。");
   return "";
  }
  string ymd = datetime.now.tostring("yyyymmdd", system.globalization.datetimeformatinfo.invariantinfo);
  dirpath += ymd + "/";
  saveurl = saveurl + ymd + "/";
  //判断目录是否存在
  if (!system.io.directory.exists(dirpath))
  {
   //创建目录
   system.io.directory.createdirectory(dirpath);
  }
  string newfilename = guid.newguid().tostring() + fileext;
  //图片名字
  string filepath = dirpath + newfilename;
  system.io.file.move(fpath, filepath);
  string fileurl = saveurl + newfilename;
  img.imageurl = fileurl;
  //imageurl = saveurl + newfilename;
  return fileurl;
 }
 public string isallowedextension(httppostedfile f)
 {
  string newfile = server.mappath("~/" + system.guid.newguid().tostring("d") + ".tmp");
  f.saveas(newfile);
  system.io.filestream fs = new system.io.filestream(newfile, system.io.filemode.open, system.io.fileaccess.read);
  system.io.binaryreader r = new system.io.binaryreader(fs);
  string fileclass = "";
  byte buffer;
  buffer = r.readbyte();
  fileclass = buffer.tostring();
  buffer = r.readbyte();
  fileclass += buffer.tostring();
  r.close();
  fs.close();
  /* 文件扩展名说明
  *7173    gif 
  *255216   jpg
  *13780    png
  *6677    bmp
   */
  dictionary<string, string> ftype = new dictionary<string, string>();
  //添加允许的文件类型
  ftype.add("7173", "gif");
  ftype.add("255216", "jpg");
  ftype.add("13780", "png");
  ftype.add("6677", "bmp");
  if (ftype.containskey(fileclass))
  {
   return newfile;
  }
  else
  {
   system.io.file.delete(newfile);
   return "";
  }
 }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
</head>
<body>
 <form id="form1" runat="server">
 <asp:fileupload id="fuimg" runat="server" />
 <asp:button id="button1" runat="server" 
 onclick="button1_click" text="上传测试" />
 <asp:image id="img" runat="server" />
 </form>
</body>
</html>

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网