File Upload Regular Expression Validator Error Message Shows After Upload

Posted on 2006-07-19
Last Modified: 2008-01-09
Hello. I have a regular expression validator for my file upload control that only allows jpgs and gifs:


The problem is that after I upload the file, the validator error message is displayed.

I also noticed that when I add the following for the Upload Button Event:

If Page.IsValid Then
 'UPLOAD Files
End If

I am never able to upload a file even if I am uploading a valid file (.jpg, .gif, etc).  However, when I don't add the page.isvalid part, everything works (except that the error message is displayed after I upload).

I'm really scratching my head on this, so help will really be appreciated.

Question by:Skytide
  • 3

Expert Comment

ID: 17143646
re check the formation of your if.. else... end if  condition

it would be nice to see the code or parts of it so we could help in tracing it..

Author Comment

ID: 17143826
Hello. Here is the button click code:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FileUpload1.HasFile Then
            Dim myFilePath As String = Server.MapPath("~/test") & "/" & FileUpload1.FileName
            Dim myImage As Image = Image.FromStream(FileUpload1.PostedFile.InputStream)

            If FileUpload1.PostedFile.ContentLength < 500000 Then
                Label1.Text = "Received " & FileUpload1.FileName & " Content Type " & FileUpload1.PostedFile.ContentType & " Length " & FileUpload1.PostedFile.ContentLength

                If myImage.Width > 500 Or myImage.Height > 500 Then
                    Dim o As New ImageHelper.ImageResize
                    o.File = myFilePath

                    'Determines whether width or height is set to be resized base on the largest value.
                    If myImage.Width > myImage.Height Then
                        o.Width = 500
                        o.Height = 500
                    End If

                    If FileUpload1.PostedFile.ContentType = "image/jpeg" Or FileUpload1.PostedFile.ContentType = "image/pjpeg" Or FileUpload1.PostedFile.ContentType = "image/jpg" Then
                        o.GetThumbnail.Save(myFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
                    ElseIf FileUpload1.PostedFile.ContentType = "image/gif" Then
                        o.GetThumbnail.Save(myFilePath, System.Drawing.Imaging.ImageFormat.Gif)
                    End If

                    If (FileUpload1.PostedFile.ContentType = "image/jpeg" Or FileUpload1.PostedFile.ContentType = "image/pjpeg" Or FileUpload1.PostedFile.ContentType = "image/jpg") _
                        And (FileUpload1.PostedFile.ContentLength > 30000) Then
                        SaveJPGWithCompressionSetting(myImage, myFilePath, 60)
                    End If
                End If 'myImage.Width > 500 Or myImage.Height > 500

                Label1.Text = "The File is too large."
            End If 'FileUpload1.PostedFile.ContentLength < 500000

            Label1.Text = "No uploaded file"
        End If
    End Sub

Author Comment

ID: 17143836
Here is the HTML Code too:

    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:RegularExpressionValidator ID="FileUploadValidator" runat="server" ControlToValidate="FileUpload1"
            ErrorMessage="Upload Jpegs and Gifs only." ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.jpeg|.JPEG|.gif|.GIF)$" Display="Dynamic"></asp:RegularExpressionValidator><br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Upload" /><br />

Author Comment

ID: 17188189
I figured out an easier way:


That works great.

Accepted Solution

Netminder earned 0 total points
ID: 17220398
Closed, 500 points refunded.
Site Admin

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question