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

x
?
Solved

File Upload Regular Expression Validator Error Message Shows After Upload

Posted on 2006-07-19
6
Medium Priority
?
907 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
  • 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

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