惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
博客园_首页
月光博客
月光博客
N
News | PayPal Newsroom
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
G
Google Developers Blog
T
Troy Hunt's Blog
博客园 - Franky
腾讯CDC
S
Security Affairs
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
D
DataBreaches.Net
Recorded Future
Recorded Future
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
D
Docker
P
Proofpoint News Feed
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

博客园 - Dawnxu

修改的Flash + ASP.NET 拍照程序 AjaxPro在Windows 2000 server下不能正常使用的解决方案 MSN 8.0 邀请函! RAR 命令 新版XNet.SqlHelper 很不错的Windows 控件 Developer Express Inc.NET 鼠标按住任意控件拖动窗口 61条面向对象设计的经验原则 两个WEB程序之间的通信 本人还有50份GMail邀请函,要的请留言! 关于俱乐部活动的个人想法! 操作MS SQL Server 存储过程的类(外加ASP.NET MessageBox类) 修改的一个Title提示 大学毕业了再看你会后悔一辈子的 妙用Asp.Net中的HttpHandler [转载]ASP.NET分页的处理方式 [转载]ASP.NET开发经验积累 WINDOWS2000开机硬盘自动共享 C#实现的根据年月日计算星期几的函数
[转载]无组件上传程序ASP.NET版
Dawnxu · 2005-05-30 · via 博客园 - Dawnxu

  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 = TrueAs 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 StringByVal strExtName As StringAs 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(11000)
169 
170         GetRandomFileName = tempFileName & strExtName
171 
172     End Function
173 
174     '----------------------------------------------------------------------------------------
175     '功能说明:获取指定上下限内的随机数
176     '-----------------------------------------------------------------------------------------
177 
178     Public Function GetRandomNumber(Optional ByVal Low As Integer = 1Optional ByVal High As Integer = 100As 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 StringAs String
191         Return Right(strFileName, InStr(StrReverse(strFileName), "."))
192     End Function
193 
194 End Class
195