Uploading a Document using Visualforce and a Custom Controller
April 22nd, 2010
The Salesforce docs for the inputFile Visualforce component has an example of uploading a document using the Standard Controller. Here is a quick example of using a Custom Controller in case you want to make the upload process part of a larger transaction.
![]()
Make sure you take a look at the finally block in the controller below. The finally block always executes when the try block exits regardless if an error occurs or not. You need to ensure you clear out document’s body (document.body = null) so that the blob is not automatically included in the serialized image of the controller. If you do not clear out the body, you’ll get the following view state error:
Maximum view state size limit (128K) exceeded. Actual viewstate size for this page was…
FileUploadExample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <apex:page controller="FileUploadController">
<apex:sectionHeader title="Visualforce Example" subtitle="File Upload Example"/>
<apex:form enctype="multipart/form-data">
<apex:pageMessages />
<apex:pageBlock title="Upload a File">
<apex:pageBlockButtons >
<apex:commandButton action="{!upload}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection showHeader="false" columns="2" id="block1">
<apex:pageBlockSectionItem >
<apex:outputLabel value="File Name" for="fileName"/>
<apex:inputText value="{!document.name}" id="fileName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!document.description}" id="description"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Keywords" for="keywords"/>
<apex:inputText value="{!document.keywords}" id="keywords"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page> |
FileUploadController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public with sharing class FileUploadController {
public Document document {
get {
if (document == null)
document = new Document();
return document;
}
set;
}
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;
}
} |
Categories: Apex, Code Sample, Salesforce, Visualforce













Couldn’t you just declare document transient so it’s not transmitted in the viewstate?
Chris, that might work but I’m not sure. I tried it quickly (don’t have the time right now) after making some changes and it threw a npe. Can you get it to work?
[...] is a follow up post to Uploading a Document using Visualforce and a Custom Controller showing an example for uploading an attachment for a Contact. The Visualforce page and Controller [...]
Hi Jeff,
What i tried to do is upload an image file and display the same using a visual force page and was successful in doing that. But when i tried to do the same using Customer portal user license and Partner portal user license i found out that these users can only read the documents and cannot edit or create. Have you ever hit any kind of requirement like this? Is there any workaround ?
I have not as that would probably violate licensing agreements.
Jeff,
Nice example. Do you have any example on doing a multipart post of a document from an Apex class to an external site. Or have you come across any in other blogs? I really appreciate your help on this. Thanks.
Sorry @Murthy I have not seen this anywhere.
Great Example Jeff. Thanks for posting this on to website.
Thanks for the tip about the “finally” keyword. I was nulling out file bodies in other spots before the “return” statement, but kept running into the ViewState error. Putting it into the “finally” block cleared it right up!