Rice farmers without component upload program asp.net version

Source: Internet
Author: User
Tags file upload integer net return save file
Asp.net| Program | upload | no components

Upload in the Web development, is a very common task, the previous use of ASP, has been using rice farmers without component upload tool, feel very good, and now learn asp.net, but found no similar to the original rice farmers without component upload program, so spent a little time, Rice farmers without components upload program with vb.net rewrite, you can compile into a DLL, in C # or vb.net, such as arbitrary asp.net support language used in the sharing out, hope to save a little time for everyone, also welcome suggestions and suggestions, let him more perfect.

Option Explicit on
Option Strict on

Imports System.IO
Imports System.Data
Imports System.Web.UI.HtmlControls.HtmlInputControl


Public Class UploadFile

'-------------------------------------------------------------------
' Welcome reprint, but please keep the following reputation
' Description: File Upload class
' Creation time: 2004-11-18
' Author: Tip Guest qq:51978456
--------------------------------------------------------------------

Private Locorgfilename as String ' original filename
Private Locnewfilename as String ' new filename
Private Locuploaddir as String = "' Save directory Note: to full path
Private locallowoverwrite as Boolean = False ' If the save file already exists, overwrite
Private locallowextfile as String = "doc,jpg,gif,zip" ' license format
Private locallowmaxsize as Integer = 3 * 1024 * 1024 ' license size

Error prompt returned by 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

    '----------------------------------------------------------------------------------------
    ' function Description: Uploading files to server
    ' parameters: file to upload the domain israndom whether to use random number file name, if the flase, Then use the original name of the file
    '----------------------------------------------------------------------------- -----------
    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 = "No upload file"
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 = "Upload directory" & Locuploaddir & "does not exist"
            Else
                 If objfile.exists (strnewfilename) and locallowoverwrite = False Then
                     errmsg = "Sorry, this file already exists on the server!"
                     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 = "Sorry, Only allow uploading of files less than "& locallowmaxsize &" bytes, uploading files beyond the maximum allowable size! "
                             return False
                         End If

Else
ErrMsg = "Sorry, only allow uploading files in the following format" & Locallowextfile & "!"
Return False
End If

End If
End If

Else
errmsg = "Upload directory not set"
Return False
End If

Return True


End Function


'-----------------------------------------------------------------------------------------
' Function Description: Get a duplicate random number file name based on date and time
'-----------------------------------------------------------------------------------------
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

'----------------------------------------------------------------------------------------
' Function Description: Gets the random number within the specified upper and lower limits
'-----------------------------------------------------------------------------------------

Public Function GetRandomNumber (Optional ByVal-As Integer = 1, Optional ByVal high As Integer = m) 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

'------------------------------------------------------------------------------------
' Function Description: According to filename, get extension
'------------------------------------------------------------------------------------

Public Function Getextname (ByVal strFileName As String) as String
return right (strFileName, InStr (StrReverse (strFileName), "."))
End Function

End Class




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.