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
Sandhyarani S PSandhyarani S P 

Visualforce Error:

Visualforce Error
Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 158.109KB 

Hi all,
I am getting this error when i try to import large file using visual force page, can anyone please tell me how to show this error like "Please selct the file less than 100kb".
Swarvi KumariSwarvi Kumari
Hi  Sandhya,

     Once try this code.
 
<apex:page standardController="Contact" showHeader="true" extensions="PhotoUploadController">
  <apex:sectionHeader title="Upload photo of {!obj.name}"/>   
    <apex:form id="theForm" >
    <apex:pagemessages />
        <br/>
        <apex:outputText ><b>1. Select the photo</b></apex:outputText><br/><br/> &nbsp; &nbsp; &nbsp;
        <apex:outputText > Click the <b>"Choose File" </b>to find the photo.</apex:outputText><br/><br/>&nbsp; &nbsp; &nbsp;
        <apex:inputFile title="Upload Photo" contentType="{!objAttach.ContentType}" value="{!objAttach.body}" fileName="{!objAttach.name}" id="file"></apex:inputFile>
        <br/><br/><br/>
        <apex:outputText ><b>2. Upload Photo</b></apex:outputText><br/><br/>&nbsp; &nbsp; &nbsp;
        <apex:outputText >Click the <b>"Upload Photo"</b> button.</apex:outputText><br/><br/>&nbsp; &nbsp; &nbsp;
        <apex:commandButton value="Upload Photo" action="{!savePhoto}" status="statusDetail"/><br/><br/>
    </apex:form>   
</apex:page>
Controller:
<apex:page standardController="Contact" showHeader="true" extensions="PhotoUploadController">
  <apex:sectionHeader title="Upload photo of {!obj.name}"/>   
    <apex:form id="theForm" >
    <apex:pagemessages />
        <br/>
        <apex:outputText ><b>1. Select the photo</b></apex:outputText><br/><br/> &nbsp; &nbsp; &nbsp;
        <apex:outputText > Click the <b>"Choose File" </b>to find the photo.</apex:outputText><br/><br/>&nbsp; &nbsp; &nbsp;
        <apex:inputFile title="Upload Photo" contentType="{!objAttach.ContentType}" value="{!objAttach.body}" fileName="{!objAttach.name}" id="file"></apex:inputFile>
        <br/><br/><br/>
        <apex:outputText ><b>2. Upload Photo</b></apex:outputText><br/><br/>&nbsp; &nbsp; &nbsp;
        <apex:outputText >Click the <b>"Upload Photo"</b> button.</apex:outputText><br/><br/>&nbsp; &nbsp; &nbsp;
        <apex:commandButton value="Upload Photo" action="{!savePhoto}" status="statusDetail"/><br/><br/>
    </apex:form>   
</apex:page>

Thanks,
Swetha.B

      
Sandhyarani S PSandhyarani S P
Hi Swetha, For controller also you are given same code.
Swarvi KumariSwarvi Kumari
Hi sandhya.



Controller :-
public with sharing class PhotoUploadController{
    public Attachment objAttach{  get {
                                        if(objAttach== null)
                                          objAttach= new Attachment();
                                        return objAttach;
                                     }
                                   set;
                                 }

    Public Contact obj{get;set;}

     public PhotoUploadController(ApexPages.StandardController controller){
        objAttach=new Attachment();
        obj= (Contact)controller.getRecord();
        if(obj.id!=null){
            obj= [select id,name from Contact where id=:obj.id];
        }
     }

     public pageReference savePhoto(){  

        String contentType=objAttach.ContentType;

        if(contentType == 'image/jpeg' || contentType== 'image/png' ){

            if(objAttach.body!=null && objAttach.name!=null){
                objAttach.parentId=obj.id;                       
                try {
                     upsert objAttach;
                     obj.Image_URL__c=URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+objAttach.id;
                     upsert obj;
                }catch (DMLException e) {
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment:'+e));
                    return null;
                }finally {
                  objAttach.body = null;
                }    
          }
          return new pagereference('/'+obj.id);                  
        }else{
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please, Upload image file with extension .JPG or .PNG'));
              return null;
       }
     }
}