2011-11-21 74 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瀏覽器也只發送文件名。其目的是爲了保護用戶的隱私(即:如果文件存儲在「我的文檔」窗口下,則可以獲得系統用戶名)。