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
priyanshipriyanshi 

Maximum View State error

Hi

 

I am trying to upload an image as an attachment in my VF page but get the maximum view state reached error when I try to upload the file. My code is as follows:

 

VF:

<tr>
<td>{!companyLogoLabel} </td>
<td>
<apex:image id="theImage" value="{!$Site.Prefix}/servlet/servlet.FileDownload?file={!attachID}" width="80" height="80" /> <br/>
<apex:outputPanel >
<div class="fileinput"><apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file" style="font-size: 12px"/>
<apex:commandButton value="{!uploadLabel}" action="{!upload}" /></div>
</apex:outputPanel>
</td>
</tr>

 

Apex:

public Attachment attachment //for company logo
{
get
{
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}

public pagereference upload() // used for uploading company logo
{
if(attachID != null)
{
list<attachment> attList = [select id from attachment where id=: attachID and id !=: oldattId];
delete attList;
}
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = accId; // the record the file is attached to
attachment.contentType ='image/jpg';
attachment.name ='logo.jpg';
try
{
insert attachment;
attachID = attachment.id;
}
catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}
finally {
attachment.body=null; //to clear the viewstate
attachment = new Attachment();
}
return null;
}

 

 

Please help.

 

Thanks

Priyanshi

RockzRockz

Hi..

 

I hope this will helps you...

 

http://boards.developerforce.com/t5/Visualforce-Development/Error-Maximum-view-state-size-limit-128K-exceeded-uh-oh/td-p/87434

 

http://boards.developerforce.com/t5/Visualforce-Development/view-state-size-exceeded-any-ideas/m-p/424329#M48866

 

Please accept my answer as a solution if my solution was helpful. This will make it available to others as a proper answer. If you felt that I went above and beyond please give me Kudos by clicking on on the star icon.

 

Thanks,

Cool Sfdc