最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#檢測(cè)上傳文件真正類型的方法

 更新時(shí)間:2015年04月25日 17:26:23   作者:gogo  
這篇文章主要介紹了C#檢測(cè)上傳文件真正類型的方法,可有效的防止用戶通過修改后綴名來改變文件類型的功能,需要的朋友可以參考下

本文實(shí)例講述了C#檢測(cè)上傳文件真正類型的方法。分享給大家供大家參考。具體分析如下:

對(duì)于用戶上傳的文件如果只是根據(jù)擴(kuò)展名判斷,很容易上傳上來可執(zhí)行文件,這是非常危險(xiǎn)的,這段代碼可以在服務(wù)器端檢測(cè)上傳文件的真實(shí)類型。

<%@ 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("請(qǐng)選擇文件。");
   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 "";
  }
  //驗(yàn)證文件格式
  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))
  {
   //創(chuàng)建目錄
   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();
  /* 文件擴(kuò)展名說明
  *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="上傳測(cè)試" />
 <asp:Image ID="Img" runat="server" />
 </form>
</body>
</html>

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

固始县| 南阳市| 镇雄县| 固镇县| 长治市| 二连浩特市| 千阳县| 栾川县| 循化| 安康市| 天等县| 阳朔县| 根河市| 新蔡县| 张家口市| 施秉县| 西宁市| 绍兴县| 隆昌县| 大石桥市| 全州县| 耒阳市| 宁乡县| 临沧市| 宁夏| 青州市| 阳江市| 丹阳市| 榆树市| 通渭县| 洛川县| 大关县| 庐江县| 皮山县| 德保县| 玉山县| 恭城| 澄迈县| 邯郸县| 重庆市| 商丘市|