function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

File Upload Query



Can anyone tell me how to get the extension of the file being uploaded in the documents tab ?


Hi you can try this,


<apex:page controller="FileUploadController" showHeader="false">
<apex:sectionHeader subtitle="Attach Documents"/>

<apex:form enctype="multipart/form-data">
<apex:pageMessages />
<apex:pageBlock title="Upload a File">

<apex:pageBlockButtons location="Bottom">
<apex:commandButton action="{!upload}" value="Save"/>

<apex:pageBlockSection showHeader="false" columns="1" id="block1">

<apex:pageBlockSectionItem >
<apex:outputLabel value="File Name" for="fileName"/>
<apex:inputText value="{!}" id="fileName"/>

<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!document.body}" filename="{!}" id="file"/>

<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!document.description}" id="description"/>

<apex:pageBlockSectionItem >
<apex:outputLabel value="Keywords" for="keywords"/>
<apex:inputText value="{!document.keywords}" id="keywords"/>





public with sharing class FileUploadController {

public Document document {
get {
if (document == null)
document = new Document();
return document;

public PageReference upload() {

document.AuthorId = UserInfo.getUserId();
document.FolderId = UserInfo.getUserId(); // put it in running user's folder

try {
insert document;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
return null;
} finally {
document.body = null; // clears the viewstate
document = new Document();

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
return null;


Gaurav RajGaurav Raj

There's no restriction to the files getting uploaded. I want to restrict certain files like .dll, .bat etc. from getting uploaded.


Hi ,


I've already iimplemented a similar code .. I m afraid but it does not satisfy our purpose ..


I want to get the extension of the file being uploaded in order to restrict some file types from being uploaded ( exe , dll etc )