验证图片的真实类型
代码如下:
|
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
|
/// <summary>/// 验证图片的真实类型,并尝试保存图片/// </summary>/// <param name="hifile">FileUpload 控件ID</param>/// <param name="savedPath">要保存图片的虚拟路径(虚拟路径不能包括扩展名,正确的写法比如"/video/2010/5/中国人"),如果上传失败,则保存的路径为空</param>/// <param name="errorMessage">如果上传失败,错误消息</param>/// <param name="allowFileEmpty">是否允许FileUpload没有文件,如果允许,则当FileUpload为空时,不显示错误信息</param>/// <param name="limitImgSize">是否限制图片的大小,true表示限制</param>/// <param name="imgSize_MB">如果限制图片的大小成立,即参数“limitImgSize”的值为True,则最大能允许的图片的兆数(MB)</param>/// <returns>验证结果,true表示成功,false表示失败</returns>public static bool ValidateImgExtensionAndSave(FileUpload hifile, ref string savedVirtualPath, out string errorMessage, bool allowFileEmpty, bool limitImgSize, double imgSize_MB){ if (hifile != null && hifile.HasFile) { Stream streamInput = hifile.PostedFile.InputStream; BinaryReader r = new BinaryReader(streamInput); string fileclass = ""; byte buffer; try { buffer = r.ReadByte(); fileclass = buffer.ToString(); buffer = r.ReadByte(); fileclass += buffer.ToString(); //r.Close(); } catch (Exception) { r.Close(); streamInput.Close(); errorMessage = "读取文件流失败,请确定您文件的正确性!"; savedVirtualPath = string.Empty; return false; } //说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar if (fileclass == "255216" || fileclass == "7173" || fileclass == "6677" || fileclass == "13780") { //成功 if (limitImgSize) { //限制图片的大小 double imgSize = (double)streamInput.Length / (1024 * 1024); if (imgSize > imgSize_MB) { errorMessage = "图片的大小最大不能超过" + imgSize_MB + "M"; savedVirtualPath = string.Empty; streamInput.Close(); return false; } } string imgExtension = string.Empty; System.Drawing.Imaging.ImageFormat imgFormat; switch (fileclass) { case "255216": imgExtension = ".jpg"; imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; case "7173": imgExtension = ".gif"; imgFormat = System.Drawing.Imaging.ImageFormat.Gif; break; case "6677": imgExtension = ".bmp"; imgFormat = System.Drawing.Imaging.ImageFormat.Bmp; break; case "13780": imgExtension = ".png"; imgFormat = System.Drawing.Imaging.ImageFormat.Png; break; default: imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; } System.Drawing.Image img = System.Drawing.Image.FromStream(streamInput); if (savedVirtualPath.LastIndexOf('/') == (savedVirtualPath.Length - 1)) { throw new Exception("没有文件名,正确的格式应该比如为:/目录名/.../myImg,其中\"myImg\"表示文件名!"); } savedVirtualPath = savedVirtualPath + imgExtension; string physicalImgPath = HttpContext.Current.Server.MapPath(savedVirtualPath); string physicalImgDictionaryPath = physicalImgPath.Substring(0, physicalImgPath.LastIndexOf('\\')); if (!Directory.Exists(physicalImgDictionaryPath)) { Directory.CreateDirectory(physicalImgDictionaryPath); } img.Save(HttpContext.Current.Server.MapPath(savedVirtualPath), imgFormat); img.Dispose(); streamInput.Close(); errorMessage = string.Empty; return true; } else { errorMessage = "图片格式不正确!"; savedVirtualPath = string.Empty; return false; } } else { savedVirtualPath = string.Empty; if (allowFileEmpty) { //允许上传的文件为空 errorMessage = string.Empty; return true; } else { //一定要有文件 errorMessage = "没有文件,或文件为空!"; return false; } }} |
转自:http://www.cnblogs.com/Music/archive/2010/08/26/ValidateImgExtensionAndSave.html

浙公网安备 33010602011771号