How to upload files to an outside service, larger than 3M? And this error on uploading: Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 156.719KB

<apex:page standardController="Employee__c" extensions="EmployeeUploadPhotoController">
 <apex:form >
    <apex:pageBlock >
         <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!saveEmp}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        <apex:pageBlockSection columns="1" showHeader="true">     
            <apex:sectionHeader title="Employee" subtitle="Upload Photo"/>                                   
            <apex:inputFile title="Upload Photo" value="{!body}" fileName="{!name}" accept="png,jpg,bmp,jpeg" id="file"></apex:inputFile>           



public class EmployeeUploadPhotoController{

    public Blob body{get; set;}
    public String name{get; set;}
    public Attachment objAttach{get;set;}
    public Employee__c objEmp{get;set;}

    public EmployeeUploadPhotoController(ApexPages.StandardController controller) {
        objAttach=new Attachment();
        objEmp = (Employee__c)controller.getRecord();
            objEmp = [select id from Employee__c where id=:objEmp.id ];

    public pagereference saveEmp(){                
        try {
            upsert objEmp;
            if(objAttach.body!=null && objAttach.name!=null){       
                upsert objAttach;
                upsert objEmp;

        catch(Exception e) {            
             return null;

        return new pagereference('/'+objEmp.id);                
  • 1
    This is a view state issue. You can post your relevant VF & apex class snippet for better answers. Feb 1 '17 at 12:39
  • can you tell me what changes i should do in controller so that the error will not occur. Feb 2 '17 at 5:26

You have already cleared out doc.body (objAttach.body=null;) to insure that the blob is not included automatically in the serialized image of the standard controller. So I don't really see any problem anymore. Tested :)

Larger than 3MB image


Have a look at this article.

An Introduction to Visualforce View State

The way to resolve this error is to use the transient keyword for the variable that is holding the value of the image

public Transient blob body{get;set;}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.