2011-11-21 131 views
0

我使用FileUpload服务器控件创建了上传页面。我使用正则表达式验证器来验证文件扩展名。验证在Firefox中上传文件的文件扩展名

<asp:FileUpload ID="AttachmentUpload" CssClass="text" size="58" Width="376px" IE:Width="385px" runat="server"/> 
        <asp:RequiredFieldValidator SetFocusOnError="true" 
               ID="AttachmentUploadRequire" 
               runat="server" 
               ControlToValidate="AttachmentUpload" 
               Display="None" 
               ErrorMessage="Please select a file to attach."/> 
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
                runat="server" 
                ErrorMessage="The selected file type is not allowed!" 
                ControlToValidate="AttachmentUpload" 
                Display="None" 
                ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(\.[mM][pP]3|\.[mM][pP][eE][gG]|\.[sS][wW][fF]|\.[dD][oO][cC]|\.[tT][xX][tT]|\.[jJ][pP][gG]|\.[jJ][pP][eE][gG]|\.[pP][nN][gG]|\.[xX][lL][sS]|\.[pP][dD][fF]|\.[gG][iI][fF]|\.[pP][pP][tT])$"/> 

它是确定的Chrome和IE,但不正常的Firefox浏览器。我该如何解决它?

+1

Firefox怎么不行?你看到一个错误?这是不是在做你期望的? – Tim

+0

正则表达式验证程序正在精细地处理其他浏览器。但验证器不在Firefox上工作。我的意思是错误警告框不显示。 – zanhtet

回答

2

考虑使用Javascript函数和在窗体按钮的OnClientClick事件上调用它。这种方法适用于所有的浏览器:

function checkFileExtension() { 
    var filePath = document.getElementById('AttachmentUpload').value; 

    var validExtension = 'xml'; 
    var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase(); 
    if (ext.toLowerCase() == validExtension) 
     return true; //xml file is valid 

    alert('The file extension ' + ext.toUpperCase() + ' is not allowed!'); 
    return false; //all other types of files are not valid 
} 
2

Firefox只提供文件名信息,而不提供其路径信息。您的正则表达式解析路径信息,因此失败。

我相信其他非IE浏览器也只发送文件名。其目的是为了保护用户的隐私(即:如果文件存储在“我的文档”窗口下,则可以获得系统用户名)。