asp.net|程式|上傳|無組件
上傳在Web開發中,是非常普遍的一項任務,以前用ASP的時候,一直用稻農的無組件上傳工具,覺得很好用,現在學Asp.net了,卻發現沒有類似原來稻農的無組件上傳程式,因此花了點時間,將稻農的無組件上傳程式用vb.net改寫了一下,可以編譯成DLL,在C#或者Vb.net等任意asp.net支援的語言中使用,在共用出來,希望能為大家節約點時間,也歡迎提出意見和建議,讓他更完善。
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Data
Imports System.Web.UI.HtmlControls.HtmlInputControl
Public Class UploadFile
'-------------------------------------------------------------------
'歡迎轉載,但請保留以下聲名
'說明:檔案上傳類
'建立時間:2004-11-18
'作者:刀尖客 QQ:51978456
--------------------------------------------------------------------
Private LocOrgFileName As String '原始檔案名
Private LocNewFileName As String '新檔案名稱
Private LocUploadDir As String = "" '儲存目錄 注意:要完整路徑
Private LocAllowOverWrite As Boolean = False '如果儲存檔案已經存在,是否覆蓋
Private LocAllowExtFile As String = "doc,jpg,gif,zip" '許可格式
Private LocAllowMaxSize As Integer = 3 * 1024 * 1024 '許可大小
Public ErrMsg As String '返回的錯誤提示
Public ReadOnly Property OrgFileName() As String
Get
Return LocOrgFileName
End Get
End Property
Public Property NewFileName() As String
Get
Return LocNewFileName
End Get
Set(ByVal strValue As String)
LocNewFileName = strValue
End Set
End Property
Public Property UploadDir() As String
Get
Return LocUploadDir
End Get
Set(ByVal strValue As String)
LocUploadDir = strValue
End Set
End Property
Public Property AllowOverWrite() As Boolean
Get
Return LocAllowOverWrite
End Get
Set(ByVal blnValue As Boolean)
LocAllowOverWrite = blnValue
End Set
End Property
Public Property AllowMaxSize() As Integer
Get
Return LocAllowMaxSize
End Get
Set(ByVal intValue As Integer)
LocAllowMaxSize = intValue
End Set
End Property
Public Property AllowExtFile() As String
Get
Return LocAllowExtFile
End Get
Set(ByVal strValue As String)
LocAllowExtFile = strValue
End Set
End Property
'----------------------------------------------------------------------------------------
'功能說明:上傳檔案到伺服器
'參數:file 要上傳的檔案域 IsRandom 是否採用隨機數檔案名稱,如果為Flase,則採用檔案原來的名稱
'----------------------------------------------------------------------------------------
Public Function Upload(ByRef file As System.Web.UI.HtmlControls.HtmlInputFile, Optional ByVal IsRandom As Boolean = True) As Boolean
Dim objFile As file
Dim oldFileName As String
Dim strNewFileName As String
Dim strExtName As String
If file.PostedFile.ContentLength = 0 Then
ErrMsg = "沒有上傳檔案"
Return False
End If
oldFileName = file.PostedFile.FileName
strExtName = GetExtName(oldFileName)
If IsRandom = True Then
LocNewFileName = GetRandomFileName(oldFileName, strExtName)
strNewFileName = LocNewFileName
Else
LocNewFileName = oldFileName
strNewFileName = LocNewFileName
End If
If LocUploadDir <> "" Then
If Directory.Exists(LocUploadDir) = False Then
ErrMsg = "上傳目錄" & LocUploadDir & "不存在"
Else
If objFile.Exists(strNewFileName) And LocAllowOverWrite = False Then
ErrMsg = "對不起,伺服器上此檔案已經存在!"
Return False
Else
If InStr(LocAllowExtFile, strExtName) <> 0 Then
If file.PostedFile.ContentLength <= LocAllowMaxSize Then
Try
file.PostedFile.SaveAs(LocUploadDir & "\" & strNewFileName)
Catch ex As Exception
ErrMsg = ex.Message
Return False
End Try
Else
ErrMsg = "對不起,只允許上傳小於" & LocAllowMaxSize & "位元組的檔案,上傳檔案超出最大的允許的大小!"
Return False
End If
Else
ErrMsg = "對不起,只允許上傳以下格式的檔案" & LocAllowExtFile & "!"
Return False
End If
End If
End If
Else
ErrMsg = "上傳目錄未設定"
Return False
End If
Return True
End Function
'-----------------------------------------------------------------------------------------
'功能說明:根據日期和時間得到一個不重複的隨機數檔案名稱
'-----------------------------------------------------------------------------------------
Public Function GetRandomFileName(ByVal strFileName As String, ByVal strExtName As String) As String
Dim tempFileName As String
Dim Ext As String
tempFileName = CStr(Now())
tempFileName = Replace(tempFileName, "-", "")
tempFileName = Replace(tempFileName, ":", "")
tempFileName = Replace(tempFileName, " ", "")
tempFileName = tempFileName & GetRandomNumber(1, 1000)
GetRandomFileName = tempFileName & strExtName
End Function
'----------------------------------------------------------------------------------------
'功能說明:擷取指定上下限內的隨機數
'-----------------------------------------------------------------------------------------
Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
Dim oRandom As System.Random
oRandom = New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
Return oRandom.Next(Low, High + 1)
End Function
'------------------------------------------------------------------------------------
'功能說明:根據檔案名稱,得到副檔名
'------------------------------------------------------------------------------------
Public Function GetExtName(ByVal strFileName As String) As String
Return Right(strFileName, InStr(StrReverse(strFileName), "."))
End Function
End Class