[C#] C#检测上传文件的真正类型 →→→→→进入此内容的聊天室

来自 , 2020-09-05, 写在 C#, 查看 122 次.
URL http://www.code666.cn/view/d58f36f7
  1. 92
  2. 93
  3. 94
  4. 95
  5. 96
  6. 97
  7. 98
  8. 99
  9. 100
  10. 101
  11. 102
  12. 103
  13. 104
  14. 105
  15. 106
  16. 107
  17. 108
  18. 109
  19. 110
  20. 111
  21. 112
  22. 113
  23. 114
  24. 115
  25. 116
  26. 117
  27. 118
  28. 119
  29. <%@ Page Language="C#" %>
  30.  
  31. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  32. <script runat="server">
  33.  
  34.   void Alert(string s)
  35.   {
  36.     Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('" + s + "')", true);
  37.   }
  38.  
  39.   protected void Button1_Click(object sender, EventArgs e)
  40.   {
  41.     saveFile();
  42.   }
  43.  
  44.   protected String saveFile()
  45.   {
  46.     String MaxSize = "1024";
  47.     //最大文件大小
  48.     int imgMaxSize = Convert.ToInt32(MaxSize) * 1024 * 1024;
  49.  
  50.     HttpPostedFile imgFile = FuImg.PostedFile;
  51.     if (imgFile == null || FuImg.FileName == "")
  52.     {
  53.       Alert("请选择文件。");
  54.       return "";
  55.     }
  56.     String dirPath = Server.MapPath("~/");
  57.     string saveUrl = Page.ResolveUrl("~/");
  58.     if (!System.IO.Directory.Exists(dirPath))
  59.     {
  60.       Alert("上传目录不存在。");
  61.       return "";
  62.     }
  63.  
  64.     String fileName = imgFile.FileName;
  65.     String fileExt = System.IO.Path.GetExtension(fileName).ToLower();
  66.  
  67.     if (imgFile.InputStream == null || imgFile.InputStream.Length > imgMaxSize)
  68.     {
  69.       Alert("上传文件大小超过限制。");
  70.       return "";
  71.     }
  72.  
  73.     //验证文件格式
  74.     String fpath = IsAllowedExtension(imgFile);
  75.     if ("" == fpath)
  76.     {
  77.       Alert("图片格式不正确。");
  78.       return "";
  79.     }
  80.  
  81.     String ymd = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
  82.     dirPath += ymd + "/";
  83.     saveUrl = saveUrl + ymd + "/";
  84.     //判断目录是否存在
  85.     if (!System.IO.Directory.Exists(dirPath))
  86.     {
  87.       //创建目录
  88.       System.IO.Directory.CreateDirectory(dirPath);
  89.     }
  90.  
  91.     String newFileName = Guid.NewGuid().ToString() + fileExt;//图片名字
  92.     String filePath = dirPath + newFileName;
  93.     System.IO.File.Move(fpath, filePath);
  94.     String fileUrl = saveUrl + newFileName;
  95.     Img.ImageUrl = fileUrl;
  96.     //ImageUrl = saveUrl + newFileName;
  97.     return fileUrl;
  98.   }
  99.  
  100.   public String IsAllowedExtension(HttpPostedFile f)
  101.   {
  102.     String newFile = Server.MapPath("~/" + System.Guid.NewGuid().ToString("D") + ".tmp");
  103.     f.SaveAs(newFile);
  104.     System.IO.FileStream fs = new System.IO.FileStream(newFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  105.     System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
  106.     string fileclass = "";
  107.     byte buffer;
  108.     buffer = r.ReadByte();
  109.     fileclass = buffer.ToString();
  110.     buffer = r.ReadByte();
  111.     fileclass += buffer.ToString();
  112.     r.Close();
  113.     fs.Close();
  114.     /* 文件扩展名说明
  115.     *7173        gif
  116.     *255216      jpg
  117.     *13780       png
  118.     *6677        bmp
  119.      */
  120.     Dictionary<String, String> ftype = new Dictionary<string, string>();
  121.     //添加允许的文件类型
  122.     ftype.Add("7173", "gif");
  123.     ftype.Add("255216", "jpg");
  124.     ftype.Add("13780", "png");
  125.     ftype.Add("6677", "bmp");
  126.     if (ftype.ContainsKey(fileclass))
  127.     {
  128.       return newFile;
  129.     }
  130.     else
  131.     {
  132.       System.IO.File.Delete(newFile);
  133.       return "";
  134.     }
  135.   }
  136. </script>
  137. <html xmlns="http://www.w3.org/1999/xhtml">
  138. <head id="Head1" runat="server">
  139. </head>
  140. <body>
  141.   <form id="form1" runat="server">
  142.   <asp:FileUpload ID="FuImg" runat="server" />
  143.   <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传测试" />
  144.   <asp:Image ID="Img" runat="server" />
  145.   </form>
  146. </body>
  147. </html>
  148. //csharp/6765

回复 "C#检测上传文件的真正类型"

这儿你可以回复上面这条便签

captcha