Solved

File Upload Regular Expression Validator Error Message Shows After Upload

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

^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))\.(?i:jp(e)?g|gif)$

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.

0
Comment
Question by:Skytide
[X]
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
6 Comments
 
LVL 3

Expert Comment

by:mazkot
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..
0
 

Author Comment

by:Skytide
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
                FileUpload1.SaveAs(myFilePath)
                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
                    Else
                        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

                Else
                    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

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

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

Author Comment

by:Skytide
ID: 17143836
Here is the HTML Code too:

<body>
    <form id="form1" runat="server">
    <div>
        <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 />
    </form>
</body>
0
 

Author Comment

by:Skytide
ID: 17188189
I figured out an easier way:

(.*\.jpg)|(.*\.JPG)|(.*\.gif)|(.*\.GIF)

That works great.
0
 
LVL 5

Accepted Solution

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

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