1

这是我的代码aspx

<asp:FileUpload ID="ImageUpload" runat="server" Style="margin-bottom: 5px" />
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
 ErrorMessage="Only jpeg, png or gif files are allowed!"
 ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
 +(.jpeg|.JPEG|.png|.PNG|.gif|.GIF)$" 
 ControlToValidate="ImageUpload"></asp:RegularExpressionValidator>

我想过滤 jpeg、png 和 gif 文件只在我的ImageUpload. 但是,当我上传 jpeg、png 和 gif 文件时,RegularExpressionValidator总是显示错误消息。我想知道我的代码有什么问题!

4

2 回答 2

1

If all you want is to assert the extension then you could try something like this:

\.(jpeg|JPEG|png|PNG|gif|GIF)$

This would match anything that ends in a . followed by either of the above extensions.

Edit:

Without having tried it and having no experience with aspx, I think you should be able to use (?i:) to make it case insensitive:

(?i:\.jpeg|png|gif)$
于 2013-04-05T10:53:47.293 回答
0

你应该试试这个

protected void Button1_Click(object sender, EventArgs e)
        {
            string filename = fileUpload.PostedFile.FileName;
            string extsn = Path.GetExtension(filename);
            if (extsn.ToUpper() == ".JPEG" || extsn.ToUpper() == ".PNG" || extsn.ToUpper() == ".GIF")
            { 

            }
        }
于 2013-04-05T11:06:58.647 回答