RegularExpressionValidator验证FileUpload(转)

ASP.NET 2.0 Life savior - RegularExpressionValidator for FileUpload control

Why did I say it was a life savior? I believe this tip is very handy for everyone. This was a problem faced by colleague. So I took up the challenge to look into this.

If you browse the internet you will find a lot of people recommending using RegularExpression Validator to validate the FileUpload control on certain file formats to be used. Some disagree on this and recommend on ServerValidate. Take note, I am not here to debate on which one to use. I am here to blog on just RegularExpressionValidator on FileUpload control and explains the best way to achieve the right result. Let me know what you think of this J

Below is the sample code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:Button ID="btnDeleteImg" runat="server" Text="Delete Image" OnClick="btnDeleteImg_Click" /><br />

            <asp:FileUpload ID="fUpload" runat="server" Width="550px" />&nbsp;

            <asp:Button ID="btnUpload" runat="server" Text="Upload" Width="76px" OnClick="btnUpload_Click" />&nbsp;

            <asp:RegularExpressionValidator ID="refImage" SetFocusOnError="True" runat="server" ControlToValidate="fUpload"

                ErrorMessage="Upload Jpegs and Gifs only" ValidationExpression="^(([a-zA-Z]:)|(""{2}"w+)"$?)(""("w["w].*))(.jpg|.JPG|.gif|.GIF)$"

               Display="Dynamic"></asp:RegularExpressionValidator>

        </div>

    </form>

</body>

</html>

 

So the expression is like this "^(([a-zA-Z]:)|(""{2}"w+)"$?)(""("w["w].*))(.jpg|.JPG|.gif|.GIF)$" which is used in this scenario.

Yes it works entirely fine on the validation. But soon later you will realize a problem.

Let’s visualize this in a table view based on the code above.

[Delete Button

[File Upload control]

[Upload Control]

[RegularExpressionValidator]

 

When you try to browse a music file, you will see an error like below:

[Delete Button

[File Upload control] c:/sgdotnet/elephant.mp3

[Upload Control]

[RegularExpressionValidator] Upload Jpegs and Gifs only

 

When you try to browse a proper image this time, the error will disappear

[Delete Button

[File Upload control] c:/sgdotnet/tortoise.jpg

[Upload Control]

[RegularExpressionValidator]

 

Now guess what when you press upload which will cause a postback, not only the image is being saved but the error message will suddenly appear. Take note, you definitely do not want to see this right.

[Delete Button

[File Upload control]

[Upload Control]

[RegularExpressionValidator] Upload Jpegs and Gifs only

** When you upload the image file, the File Upload’s textbox will be cleaned

So you will be shocked? What is the problem here? It doesn’t make sense for the error message to appear.

Then I came into a conclusion that there might be a slight possibility on the RegularExpression ValidateExpression. Take note, I am not so good in RegularExpression but I believe it helps me to achieve results here and definitely in this context.

So I came out with this instead:

"^.+".((jpg)|(gif)|(jpeg)|(png)|(bmp))$"

It solved the problem. Yes. Thanks god and I was very happy that I was able to solve this problem. Most people on the internet recommended the 1st way and honestly speaking it did validated, but the error will occurred on postback.

Have fun and hope you find this useful for you.

posted @ 2008-08-13 16:00  iLove.Net  阅读(712)  评论(0编辑  收藏  举报