End user can upload files in a Visualforce page. In the backend I need to create a list of allowed file extensions, and restrict the user to that list. How do I create the extensions list and validate it? Any help is very much appreciated.

有帮助吗?

解决方案

You don't need apex for that?

<apex:inputFile> has accept parameter which you can use. Bear in mind this will check contentType, not extension (which is bit more proper way to do it).

If you still want the validation in apex - probably something like this?

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(...);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top