locked
how to allow only .pdf files in File upload-vb.net RRS feed

  • Question

  • User-1578974752 posted

    Hi

    If the attaching file is .pdf then only ,system must allow to upload. How can I check the text after dot(.),whether it is .pdf,.jpeg or .pn etc. Appreciate the help.Thanks

    If FileUpload1.text = .pdf Then

    upload.enabled =True

    End If

    Monday, March 2, 2020 8:28 AM

Answers

  • User503812343 posted

    try below code on button click event

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim validFileTypes As String() = {"pdf"}
        Dim ext As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)
        Dim isValidFile As Boolean = False
    
        For i As Integer = 0 To validFileTypes.Length - 1
    
            If ext = "." & validFileTypes(i) Then
                isValidFile = True
                Exit For
            End If
        Next
    
        If Not isValidFile Then
            Label1.ForeColor = System.Drawing.Color.Red
            Label1.Text = "Invalid File. Please upload a File with extension " & String.Join(",", validFileTypes)
        Else
            Label1.ForeColor = System.Drawing.Color.Green
            Label1.Text = "File uploaded successfully."
        End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 2, 2020 10:45 AM