Tips and tricks for .NET using ASP and VB code.

Upload a Picture.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Specify the path on the server to save the uploaded picture to. MUST END WITH A TRAILING SLASH!
        Dim SavePath As String = Server.MapPath("PictureFolder\")
        If (FileUpload1.HasFile = True) Then
            'Get the correct file extension.
            Dim PictureFileExtension As String = GetPictureFileType(FileUpload1)
            If PictureFileExtension <> ".UNKNOWN" Then
                Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
                If (fileSize < 2097152) Then
                    'Delete any previously uploaded pictures with this name.
                    Dim PictureFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(SavePath, FileIO.SearchOption.SearchTopLevelOnly, Now.ToString("'Picture'yyyyMMddhhmmssfffffff") & PictureFileExtension)
                    If PictureFiles.Count > 0 Then
                        For P As UShort = 0 To PictureFiles.Count - 1
                            Dim FileData As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(PictureFiles.Item(P))
                            If FileData.Extension <> PictureFileExtension Then
                                My.Computer.FileSystem.DeleteFile(FileData.FullName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
                            End If
                        Next P
                    End If
                    SavePath += Now.ToString("'Picture'yyyyMMddhhmmssfffffff") & PictureFileExtension
                    FileUpload1.SaveAs(SavePath)
                    Response.Write("The picture was uploaded successfully.")
                Else
                    Response.Write("The picture was not accepted because it exceeds 2 megabytes in size.")
                End If
            Else
                Response.Write("The file was not a picture that can be displayed by this system.")
            End If
        Else
            Response.Write("You did not specify a picture to upload.")
        End If
        Response.Flush()
    End Sub

    Shared Function GetPictureFileType(ByRef UploadedFile As FileUpload) As String
        If IsNothing(UploadedFile.PostedFile.ContentType) Then
            Return ".UNKNOWN"
            Exit Function
        End If
        Select Case UploadedFile.PostedFile.ContentType.ToLower
            Case "image/gif"
                Return ".GIF"
            Case "image/jpeg"
                Return ".JPG"
            Case "image/pjpeg"
                Return ".JPG"
            Case "image/x-png"
                Return ".PNG"
            Case "image/jpg"
                Return ".JPG"
            Case "image/bmp"
                Return ".BMP"
            Case "image/x-wmf"
                Return ".WMF"
            Case Else
                Return ".UNKNOWN"
        End Select
    End Function

No comments:

Post a Comment

Search This Blog