View Code
1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Text;
4 4 using System.Web;
5 5 using System.IO;
6 6 using System.Web.Configuration;
7 7 using System.Configuration;
8 8 using System.Web.Security;
9 9 using System.Web.UI.WebControls;
10 10 namespace QmxMovieBLL
11 11
12 12 {
13 13 public class UploadFileBLL
14 14 {
15 15 string filePath = ConfigurationManager.AppSettings["UploadImages"].ToString();
16 16 private string GenerateRefUploadFileName(string ext)
17 17 {
18 18 string fileName;//初始文件名
19 19 string hashedFileName;//加密过的文件名
20 20 string refUploadDest;//最终上传地址(映射过的)
21 21 int count = 0;
22 22 do
23 23 {
24 24 if(count++>=32767) throw new Exception("生成文件名失败,请重新进行此操作!");//防止死循环
25 25 fileName = DateTime.Now.ToString() + DateTime.Now.Millisecond.ToString();
26 26 hashedFileName = FormsAuthentication.HashPasswordForStoringInConfigFile(fileName, "MD5");
27 27 refUploadDest =Path.Combine(filePath, hashedFileName);
28 28 } while (File.Exists(HttpContext.Current.Server.MapPath(refUploadDest)));
29 29 return refUploadDest+ext;
30 30 }
31 31
32 32 private bool CanUpload(string ext)
33 33 {
34 34 //TODO:是否合法的扩展名
35 35 //允许上传的图片格式为jpg/jpeg、png和gif类型
36 36 if (ext.ToLower() == ".jpg" || ext.ToLower() == ".jpeg" || ext.ToLower() == ".png"
37 37 || ext.ToLower() == ".gif")
38 38 {
39 39 return true;
40 40 }
41 41 return false;
42 42 }
43 43
44 44 private bool FileTypeAllowed(FileUpload fu)
45 45 {
46 46 //是否合法的文件类型,通过FileUpload的ContentType属性来确定类型
47 47 string fileType = fu.PostedFile.ContentType.ToString().ToLower();
48 48 if (fileType == "image/pjpeg" || fileType == "image/x-png" || fileType == "image/gif") return true;
49 49 return false;
50 50
51 51 }
52 52
53 53 public string UploadFile(FileUpload fu)
54 54 {
55 55 if (fu.HasFile)
56 56 {
57 57 string fileName = fu.FileName;
58 58 //Response.Write(fu.PostedFile.ContentType.ToString().ToLower());
59 59 string ext = Path.GetExtension(fileName).ToLower();
60 60 //首先,验证是否可以上传该文件
61 61 if (!CanUpload(ext)||!(FileTypeAllowed(fu)))
62 62 {
63 63 throw new Exception("不是允许的上传文件类型!");
64 64 }
65 65
66 66 if (!Directory.Exists(HttpContext.Current.Server.MapPath(filePath)))
67 67 {
68 68 Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filePath));
69 69 }
70 70
71 71 string refDest;
72 72 try
73 73 {
74 74 refDest = GenerateRefUploadFileName(ext);
75 75 }
76 76 catch (Exception ex)
77 77 {
78 78 throw ex;
79 79 }
80 80 string realDest = HttpContext.Current.Server.MapPath(refDest);
81 81 try
82 82 {
83 83 fu.SaveAs(realDest);
84 84 }
85 85 catch (Exception)
86 86 {
87 87 throw new Exception("文件保存出错。请检查上传文件夹是否存在。");
88 88 }
89 89 return refDest;
90 90 }
91 91 else
92 92 {
93 93 throw new Exception("请选择要上传的文件。");
94 94 }
95 95 }
96 96
97 97 }
98 98 }
99
100 要实现这个可以用到FileUpload的ContentLength属性,此属性返回要上传文件的字节数。例如,可以写一个方法,设置测试大小限制为10字节:
101 /// <summary>
102 /// 检查文件的大小是否合法
103 /// </summary>
104 /// <param name="fu"></param>
105 /// <returns></returns>
106 private bool IsUploadSizeFit(FileUpload fu)
107 {
108 const int maxFileSize = 10;
109 if (fu.PostedFile.ContentLength > maxFileSize) return false;
110 return true;
111 }
112 在上文第65行位置插入如下代码:
113 //如果文件上传大小超过限制,则不允许上传
114 if (!IsUploadSizeFit(fu))
115 {
116 throw new Exception("文件大小超过允许范围!");
117 }