1         /*文件扩展名说明
 2   * 255216 jpg
 3   * 208207 doc xls ppt wps
 4   * 8075 docx pptx xlsx zip
 5   * 5150 txt
 6   * 8297 rar
 7   * 7790 exe
 8   * 3780 pdf
 9   *
10   * 4946/104116 txt
11   * 7173        gif
12   * 255216      jpg
13   * 13780       png
14   * 6677        bmp
15   * 239187      txt,aspx,asp,sql
16   * 208207      xls.doc.ppt
17   * 6063        xml
18   * 6033        htm,html
19   * 4742        js
20   * 8075        xlsx,zip,pptx,mmap,zip
21   * 8297        rar
22   * 01          accdb,mdb
23   * 7790        exe,dll
24   * 5666        psd
25   * 255254      rdp
26   * 10056       bt种子
27   * 64101       bat
28   * 4059        sgf
29   */
30 
31         public static bool IsAllowedExtension(string hifile)
32         {
33 
34             string[] extList = { "255216", "208207", "8075", "3780", "4946", "104116", "7173", "255216", "13780", "6677", "5150" };
35 
36             System.IO.FileStream fs = new System.IO.FileStream(hifile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
37             System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
38             string fileclass = "";
39             //这里的位长要具体判断.
40             byte buffer;
41             try
42             {
43                 buffer = r.ReadByte();
44                 fileclass = buffer.ToString();
45                 buffer = r.ReadByte();
46                 fileclass += buffer.ToString();
47 
48             }
49             catch
50             {
51 
52             }
53             r.Close();
54             fs.Close();
55             if (extList.Contains(fileclass))
56             {
57                 return true;
58             }
59             else
60             {
61                 return false;
62             }
63 
64         }
View Code
 1 HttpPostedFile file = context.Request.Files["Filedata"];  
 2  string uploadPath = HttpContext.Current.Server.MapPath("~/Resource/UploadFile/");
 3  if (!Directory.Exists(uploadPath))
 4                                 {
 5                                     Directory.CreateDirectory(uploadPath);
 6                                 }
 7                                 string path = uploadPath + attachmentsId0 + _Extension;
 8                                 file.SaveAs(path);
 9  if (IsAllowedExtension(path))
10      {
11 //正常执行
12 }else
13 {
14 //跳出
15 return ;
16 }
View Code