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.

转载于:https://www.cnblogs.com/chenzhip123/archive/2008/08/13/1267022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值