2014-01-23 61 views
2

最終用戶可以在Visualforce頁面上載文件。在後端,我需要創建一個允許的文件擴展名列表,並將用戶限制在該列表中。我如何創建擴展列表並驗證它?很感謝任何形式的幫助。在Apex中驗證文件擴展名

回答

1

你不需要頂點?

<apex:inputFile>accept您可以使用的參數。請記住,這將檢查contentType,而不是擴展名(這是更正確的方法來做到這一點)。

如果你仍然想在apex驗證 - 可能是這樣的?

String fileName = 'foobar.xls'; 
Set<String> acceptedExtensions = new Set<String> {'.doc','.txt', '.jpg'}; 

Boolean found = false; 
for(String s : acceptedExtensions){ 
    if(found = fileName.endsWith(s)){ // yes, there's only one "=", I do want assignment here 
     break; 
    } 
} 
if(!found){ // after the whole loop it's still false? 
    ApexPages.addMessage(...); 
} 
+0

謝謝!這很有幫助。 – Nish