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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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