稻農的無組件上傳程式ASP.NET版

來源:互聯網
上載者:User
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




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.