[转载]无组件上传程序ASP.NET版
1 Option Explicit On
2 Option Strict On
3
4 Imports System.IO
5 Imports System.Data
6 Imports System.Web.UI.HtmlControls.HtmlInputControl
7
8
9 Public Class UploadFile
10
11 '-------------------------------------------------------------------
12 '欢迎转载,但请保留以下声名
13 '说明:文件上传类
14 '创建时间:2004-11-18
15 '作者:刀尖客 QQ:51978456
16 --------------------------------------------------------------------
17
18 Private LocOrgFileName As String '原始文件名
19 Private LocNewFileName As String '新文件名
20 Private LocUploadDir As String = "" '保存目录 注意:要完整路径
21 Private LocAllowOverWrite As Boolean = False '如果保存文件已经存在,是否覆盖
22 Private LocAllowExtFile As String = "doc,jpg,gif,zip" '许可格式
23 Private LocAllowMaxSize As Integer = 3 * 1024 * 1024 '许可大小
24
25 Public ErrMsg As String '返回的错误提示
26
27 Public ReadOnly Property OrgFileName() As String
28 Get
29 Return LocOrgFileName
30 End Get
31 End Property
32
33 Public Property NewFileName() As String
34 Get
35 Return LocNewFileName
36 End Get
37 Set(ByVal strValue As String)
38 LocNewFileName = strValue
39 End Set
40 End Property
41
42 Public Property UploadDir() As String
43 Get
44 Return LocUploadDir
45 End Get
46 Set(ByVal strValue As String)
47 LocUploadDir = strValue
48 End Set
49 End Property
50
51 Public Property AllowOverWrite() As Boolean
52 Get
53 Return LocAllowOverWrite
54 End Get
55
56 Set(ByVal blnValue As Boolean)
57 LocAllowOverWrite = blnValue
58 End Set
59
60 End Property
61
62 Public Property AllowMaxSize() As Integer
63 Get
64 Return LocAllowMaxSize
65 End Get
66
67 Set(ByVal intValue As Integer)
68 LocAllowMaxSize = intValue
69 End Set
70 End Property
71
72 Public Property AllowExtFile() As String
73 Get
74 Return LocAllowExtFile
75 End Get
76
77 Set(ByVal strValue As String)
78 LocAllowExtFile = strValue
79 End Set
80 End Property
81
82
83 '----------------------------------------------------------------------------------------
84 '功能说明:上传文件到服务器
85 '参数:file 要上传的文件域 IsRandom 是否采用随机数文件名,如果为Flase,则采用文件原来的名称
86 '----------------------------------------------------------------------------------------
87 Public Function Upload(ByRef file As System.Web.UI.HtmlControls.HtmlInputFile, Optional ByVal IsRandom As Boolean = True) As Boolean
88 Dim objFile As file
89 Dim oldFileName As String
90 Dim strNewFileName As String
91 Dim strExtName As String
92
93 If file.PostedFile.ContentLength = 0 Then
94 ErrMsg = "没有上传文件"
95 Return False
96 End If
97
98 oldFileName = file.PostedFile.FileName
99
100 strExtName = GetExtName(oldFileName)
101
102
103 If IsRandom = True Then
104 LocNewFileName = GetRandomFileName(oldFileName, strExtName)
105 strNewFileName = LocNewFileName
106 Else
107 LocNewFileName = oldFileName
108 strNewFileName = LocNewFileName
109 End If
110
111 If LocUploadDir <> "" Then
112 If Directory.Exists(LocUploadDir) = False Then
113 ErrMsg = "上传目录" & LocUploadDir & "不存在"
114 Else
115 If objFile.Exists(strNewFileName) And LocAllowOverWrite = False Then
116 ErrMsg = "对不起,服务器上此文件已经存在!"
117 Return False
118 Else
119
120 If InStr(LocAllowExtFile, strExtName) <> 0 Then
121
122 If file.PostedFile.ContentLength <= LocAllowMaxSize Then
123
124 Try
125 file.PostedFile.SaveAs(LocUploadDir & "\" & strNewFileName)
126 Catch ex As Exception
127 ErrMsg = ex.Message
128 Return False
129 End Try
130
131
132 Else
133 ErrMsg = "对不起,只允许上传小于" & LocAllowMaxSize & "字节的文件,上传文件超出最大的允许的大小!"
134 Return False
135 End If
136
137 Else
138 ErrMsg = "对不起,只允许上传以下格式的文件" & LocAllowExtFile & "!"
139 Return False
140 End If
141
142 End If
143 End If
144
145 Else
146 ErrMsg = "上传目录未设置"
147 Return False
148 End If
149
150 Return True
151
152
153 End Function
154
155
156 '-----------------------------------------------------------------------------------------
157 '功能说明:根据日期和时间得到一个不重复的随机数文件名
158 '-----------------------------------------------------------------------------------------
159 Public Function GetRandomFileName(ByVal strFileName As String, ByVal strExtName As String) As String
160
161 Dim tempFileName As String
162 Dim Ext As String
163
164 tempFileName = CStr(Now())
165 tempFileName = Replace(tempFileName, "-", "")
166 tempFileName = Replace(tempFileName, ":", "")
167 tempFileName = Replace(tempFileName, " ", "")
168 tempFileName = tempFileName & GetRandomNumber(1, 1000)
169
170 GetRandomFileName = tempFileName & strExtName
171
172 End Function
173
174 '----------------------------------------------------------------------------------------
175 '功能说明:获取指定上下限内的随机数
176 '-----------------------------------------------------------------------------------------
177
178 Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
179 Dim oRandom As System.Random
180
181 oRandom = New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
182 Return oRandom.Next(Low, High + 1)
183
184 End Function
185
186 '------------------------------------------------------------------------------------
187 '功能说明:根据文件名,得到扩展名
188 '------------------------------------------------------------------------------------
189
190 Public Function GetExtName(ByVal strFileName As String) As String
191 Return Right(strFileName, InStr(StrReverse(strFileName), "."))
192 End Function
193
194 End Class
195
2 Option Strict On
3
4 Imports System.IO
5 Imports System.Data
6 Imports System.Web.UI.HtmlControls.HtmlInputControl
7
8
9 Public Class UploadFile
10
11 '-------------------------------------------------------------------
12 '欢迎转载,但请保留以下声名
13 '说明:文件上传类
14 '创建时间:2004-11-18
15 '作者:刀尖客 QQ:51978456
16 --------------------------------------------------------------------
17
18 Private LocOrgFileName As String '原始文件名
19 Private LocNewFileName As String '新文件名
20 Private LocUploadDir As String = "" '保存目录 注意:要完整路径
21 Private LocAllowOverWrite As Boolean = False '如果保存文件已经存在,是否覆盖
22 Private LocAllowExtFile As String = "doc,jpg,gif,zip" '许可格式
23 Private LocAllowMaxSize As Integer = 3 * 1024 * 1024 '许可大小
24
25 Public ErrMsg As String '返回的错误提示
26
27 Public ReadOnly Property OrgFileName() As String
28 Get
29 Return LocOrgFileName
30 End Get
31 End Property
32
33 Public Property NewFileName() As String
34 Get
35 Return LocNewFileName
36 End Get
37 Set(ByVal strValue As String)
38 LocNewFileName = strValue
39 End Set
40 End Property
41
42 Public Property UploadDir() As String
43 Get
44 Return LocUploadDir
45 End Get
46 Set(ByVal strValue As String)
47 LocUploadDir = strValue
48 End Set
49 End Property
50
51 Public Property AllowOverWrite() As Boolean
52 Get
53 Return LocAllowOverWrite
54 End Get
55
56 Set(ByVal blnValue As Boolean)
57 LocAllowOverWrite = blnValue
58 End Set
59
60 End Property
61
62 Public Property AllowMaxSize() As Integer
63 Get
64 Return LocAllowMaxSize
65 End Get
66
67 Set(ByVal intValue As Integer)
68 LocAllowMaxSize = intValue
69 End Set
70 End Property
71
72 Public Property AllowExtFile() As String
73 Get
74 Return LocAllowExtFile
75 End Get
76
77 Set(ByVal strValue As String)
78 LocAllowExtFile = strValue
79 End Set
80 End Property
81
82
83 '----------------------------------------------------------------------------------------
84 '功能说明:上传文件到服务器
85 '参数:file 要上传的文件域 IsRandom 是否采用随机数文件名,如果为Flase,则采用文件原来的名称
86 '----------------------------------------------------------------------------------------
87 Public Function Upload(ByRef file As System.Web.UI.HtmlControls.HtmlInputFile, Optional ByVal IsRandom As Boolean = True) As Boolean
88 Dim objFile As file
89 Dim oldFileName As String
90 Dim strNewFileName As String
91 Dim strExtName As String
92
93 If file.PostedFile.ContentLength = 0 Then
94 ErrMsg = "没有上传文件"
95 Return False
96 End If
97
98 oldFileName = file.PostedFile.FileName
99
100 strExtName = GetExtName(oldFileName)
101
102
103 If IsRandom = True Then
104 LocNewFileName = GetRandomFileName(oldFileName, strExtName)
105 strNewFileName = LocNewFileName
106 Else
107 LocNewFileName = oldFileName
108 strNewFileName = LocNewFileName
109 End If
110
111 If LocUploadDir <> "" Then
112 If Directory.Exists(LocUploadDir) = False Then
113 ErrMsg = "上传目录" & LocUploadDir & "不存在"
114 Else
115 If objFile.Exists(strNewFileName) And LocAllowOverWrite = False Then
116 ErrMsg = "对不起,服务器上此文件已经存在!"
117 Return False
118 Else
119
120 If InStr(LocAllowExtFile, strExtName) <> 0 Then
121
122 If file.PostedFile.ContentLength <= LocAllowMaxSize Then
123
124 Try
125 file.PostedFile.SaveAs(LocUploadDir & "\" & strNewFileName)
126 Catch ex As Exception
127 ErrMsg = ex.Message
128 Return False
129 End Try
130
131
132 Else
133 ErrMsg = "对不起,只允许上传小于" & LocAllowMaxSize & "字节的文件,上传文件超出最大的允许的大小!"
134 Return False
135 End If
136
137 Else
138 ErrMsg = "对不起,只允许上传以下格式的文件" & LocAllowExtFile & "!"
139 Return False
140 End If
141
142 End If
143 End If
144
145 Else
146 ErrMsg = "上传目录未设置"
147 Return False
148 End If
149
150 Return True
151
152
153 End Function
154
155
156 '-----------------------------------------------------------------------------------------
157 '功能说明:根据日期和时间得到一个不重复的随机数文件名
158 '-----------------------------------------------------------------------------------------
159 Public Function GetRandomFileName(ByVal strFileName As String, ByVal strExtName As String) As String
160
161 Dim tempFileName As String
162 Dim Ext As String
163
164 tempFileName = CStr(Now())
165 tempFileName = Replace(tempFileName, "-", "")
166 tempFileName = Replace(tempFileName, ":", "")
167 tempFileName = Replace(tempFileName, " ", "")
168 tempFileName = tempFileName & GetRandomNumber(1, 1000)
169
170 GetRandomFileName = tempFileName & strExtName
171
172 End Function
173
174 '----------------------------------------------------------------------------------------
175 '功能说明:获取指定上下限内的随机数
176 '-----------------------------------------------------------------------------------------
177
178 Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
179 Dim oRandom As System.Random
180
181 oRandom = New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
182 Return oRandom.Next(Low, High + 1)
183
184 End Function
185
186 '------------------------------------------------------------------------------------
187 '功能说明:根据文件名,得到扩展名
188 '------------------------------------------------------------------------------------
189
190 Public Function GetExtName(ByVal strFileName As String) As String
191 Return Right(strFileName, InStr(StrReverse(strFileName), "."))
192 End Function
193
194 End Class
195