Go Premium for a chance to win a PS4. Enter to Win


File Upload Regular Expression Validator Error Message Shows After Upload

Posted on 2006-07-19
Medium Priority
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

971 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