forked from szgit/Helper.Core.Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadVerifyHelper.cs
191 lines (180 loc) · 8.39 KB
/
UploadVerifyHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* 作用:验证上传文件格式。
* 联系:QQ 100101392
* 来源:https://github.com/snipen/Helper.Core.Library
* */
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
namespace Helper.Core.Library
{
#region 逻辑辅助枚举类
public class VerifyFormatTypeEnum
{
public const string ASPX = ".aspx";
public const string BMP = ".bmp";
public const string CS = ".cs";
public const string CHM = ".chm";
public const string DLL = ".dll";
public const string DOC = ".doc";
public const string DOCX = ".docx";
public const string EXE = ".exe";
public const string GIF = ".gif";
public const string HTML = ".html";
public const string JS = ".js";
public const string JPG = ".jpg";
public const string JPEG = ".jpeg";
public const string MP4 = ".mp4";
public const string PSD = ".psd";
public const string PDF = ".pdf";
public const string PNG = ".png";
public const string RAR = ".rar";
public const string RMVB = ".rmvb";
public const string SWF = ".swf";
public const string SQL = ".sql";
public const string TXT = ".txt";
public const string XML = ".xml";
public const string XLSX = ".xlsx";
public const string XLS = ".xls";
public const string ZIP = ".zip";
}
#endregion
public class UploadVerifyHelper
{
#region 对外公开方法
/// <summary>
/// 验证上传文件是否合法
/// </summary>
/// <param name="httpPostedFile">HttpPostedFileBase</param>
/// <param name="suffixList">合法后缀列表,例:.rar</param>
/// <param name="serialTypeList">VerifyFormatSerialTypeEnum</param>
/// <returns></returns>
public static bool Verify(HttpPostedFileBase httpPostedFile, string[] suffixList, params int[] serialTypeList)
{
MemoryStream memoryStream = null;
BinaryReader binaryReader = null;
try
{
Stream stream = httpPostedFile.InputStream;
string suffix = FileHelper.GetSuffix(httpPostedFile.FileName);
if (suffixList != null && !suffixList.Contains(suffix)) return false;
Byte[] bytesContent = new Byte[2];
stream.Read(bytesContent, 0, 2);
stream.Seek(0, SeekOrigin.Begin);
memoryStream = new MemoryStream(bytesContent);
binaryReader = new BinaryReader(memoryStream);
string bufferText = string.Empty;
byte buffer = byte.MinValue;
buffer = binaryReader.ReadByte();
bufferText = buffer.ToString();
buffer = binaryReader.ReadByte();
bufferText += buffer.ToString();
foreach (int formatSerialType in serialTypeList)
{
if (int.Parse(bufferText) == formatSerialType) return true;
}
return false;
}
catch
{
throw;
}
finally
{
if (memoryStream != null) memoryStream.Dispose();
if (binaryReader != null) binaryReader.Dispose();
}
}
/// <summary>
/// 验证上传图片类型是否合法
/// </summary>
/// <param name="httpPostedFile">HttpPostedFileBase</param>
/// <returns></returns>
public static bool Verify(HttpPostedFileBase httpPostedFile)
{
return Verify(httpPostedFile, new string[] { VerifyFormatTypeEnum.JPG, VerifyFormatTypeEnum.PNG, VerifyFormatTypeEnum.GIF, VerifyFormatTypeEnum.BMP }, VerifyFormatSerialTypeEnum.JPG, VerifyFormatSerialTypeEnum.GIF, VerifyFormatSerialTypeEnum.PNG, VerifyFormatSerialTypeEnum.BMP);
}
/// <summary>
/// 根据扩展名获取 VerifyFormatSerialTypeEnum 常量数组
/// </summary>
/// <param name="extensionStr">扩展字符串,例:.jpg|.png</param>
/// <param name="splitChar">分隔符,例:|</param>
/// <returns></returns>
public static List<int> GetSerialTypeListByString(string extensionStr, string splitChar = "|")
{
List<int> resultList = new List<int>();
List<string> extDataList = StringHelper.ToList<string>(extensionStr, splitChar, true, StringCaseTypeEnum.Lower);
foreach (string extData in extDataList)
{
int serialValue = -1;
switch (extData)
{
case VerifyFormatTypeEnum.ASPX: serialValue = VerifyFormatSerialTypeEnum.ASPX; break;
case VerifyFormatTypeEnum.BMP: serialValue = VerifyFormatSerialTypeEnum.BMP; break;
case VerifyFormatTypeEnum.CS: serialValue = VerifyFormatSerialTypeEnum.CS; break;
case VerifyFormatTypeEnum.CHM: serialValue = VerifyFormatSerialTypeEnum.CHM; break;
case VerifyFormatTypeEnum.DLL: serialValue = VerifyFormatSerialTypeEnum.DLL; break;
case VerifyFormatTypeEnum.DOC: serialValue = VerifyFormatSerialTypeEnum.DOC; break;
case VerifyFormatTypeEnum.DOCX: serialValue = VerifyFormatSerialTypeEnum.DOCX; break;
case VerifyFormatTypeEnum.EXE: serialValue = VerifyFormatSerialTypeEnum.EXE; break;
case VerifyFormatTypeEnum.GIF: serialValue = VerifyFormatSerialTypeEnum.GIF; break;
case VerifyFormatTypeEnum.HTML: serialValue = VerifyFormatSerialTypeEnum.HTML; break;
case VerifyFormatTypeEnum.JS: serialValue = VerifyFormatSerialTypeEnum.JS; break;
case VerifyFormatTypeEnum.JPG: serialValue = VerifyFormatSerialTypeEnum.JPG; break;
case VerifyFormatTypeEnum.JPEG: serialValue = VerifyFormatSerialTypeEnum.JPEG; break;
case VerifyFormatTypeEnum.PSD: serialValue = VerifyFormatSerialTypeEnum.PSD; break;
case VerifyFormatTypeEnum.PDF: serialValue = VerifyFormatSerialTypeEnum.PDF; break;
case VerifyFormatTypeEnum.PNG: serialValue = VerifyFormatSerialTypeEnum.PNG; break;
case VerifyFormatTypeEnum.RAR: serialValue = VerifyFormatSerialTypeEnum.RAR; break;
case VerifyFormatTypeEnum.SWF: serialValue = VerifyFormatSerialTypeEnum.SWF; break;
case VerifyFormatTypeEnum.SQL: serialValue = VerifyFormatSerialTypeEnum.SQL; break;
case VerifyFormatTypeEnum.TXT: serialValue = VerifyFormatSerialTypeEnum.TXT; break;
case VerifyFormatTypeEnum.XML: serialValue = VerifyFormatSerialTypeEnum.XML; break;
case VerifyFormatTypeEnum.XLSX: serialValue = VerifyFormatSerialTypeEnum.XLSX; break;
case VerifyFormatTypeEnum.XLS: serialValue = VerifyFormatSerialTypeEnum.XLS; break;
case VerifyFormatTypeEnum.ZIP: serialValue = VerifyFormatSerialTypeEnum.ZIP; break;
}
if (serialValue != -1)
{
resultList.Add(serialValue);
}
}
return resultList;
}
#endregion
}
#region 逻辑处理辅助类
public class VerifyFormatSerialTypeEnum
{
public const int ASPX = 239187;
public const int BMP = 6677;
public const int CS = 117115;
public const int CHM = 7384;
public const int DLL = 7790;
public const int DOC = 208207;
public const int DOCX = 8075;
public const int EXE = 7790;
public const int GIF = 7173;
public const int HTML = 6063;
public const int JS = 119105;
public const int JPG = 255216;
public const int JPEG = 255216;
public const int PSD = 5666;
public const int PDF = 3780;
public const int PNG = 13780;
public const int PPT = 208207;
public const int PPTX = 8075;
public const int RAR = 8297;
public const int SWF = 6787;
public const int SQL = 255254;
public const int TXT = 5150;
public const int WPS = 208207;
public const int XML = 6063;
public const int XLSX = 8075;
public const int XLS = 208207;
public const int ZIP = 8075;
}
#endregion
}