0

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:pageBlockButtons>
        <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>           
        </apex:pageBlockSection>
    </apex:pageBlock>    
    </apex:form>

</apex:page>

controller-

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();
        if(objEmp.id!=null)
            objEmp = [select id from Employee__c where id=:objEmp.id ];
    }

    public pagereference saveEmp(){                
        try {
            upsert objEmp;
            objAttach.body=body;
            objAttach.name=name;
            if(objAttach.body!=null && objAttach.name!=null){       
                objAttach.parentId=objEmp.id;
                upsert objAttach;
                //objEmp.Image_URL__c='https://c.ap1.content.force.com/servlet/servlet.FileDownload?file='+objAttach.id;
                objEmp.Image_URL__c='/servlet/servlet.FileDownload?file='+objAttach.id;
                upsert objEmp;
            }
            objAttach.body=null;
        }        

        catch(Exception e) {            
             return null;
        }

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

2 Answers 2

0

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

0

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;}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .