4

So I have a file uploader using inputFile component on my VF page. It works fine except when the page rerenders it now exceeds the maximum viewstate if a file is attached. This is a problem if the user tried to submit and fails validation (such as blank required field) and the page refreshes with error messages, the viewstate exceeded exception gets thrown because of the file.

How can I avoid this?

Visualforce (relevant parts)

    <apex:form enctype="multipart/form-data">
       <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                <apex:outputLabel value="Attachments" />
                <apex:inputFile value="{!file}" id="fileupload"
                    fileName="{!UploadedfileName}" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
     </apex:form>

Apex (relevant parts)

public Blob file {get;set;} 
public String UploadedfileName{get;set;}

 public void submit() { 
        MapRequest__c cont=(MapRequest__c) std.getRecord();
    cont.Map_Type__c = getMapTypeString();
    cont.Share_Type__c = getShareTypeString();
    cont.Demographic_Options__c = getDemographicSubOptions();
    cont.Requested_Layers__c = getRequestedLayers();
    cont.Properties__c = getSelected();
    Attachment att = new Attachment(); 
    try {
      insert cont;

    if (file != null){ 

        att.Name = UploadedfileName; 
        att.Body = file;
        att.ParentId = cont.Id; 
        att.Description = 'File Uploaded'; 
        insert att;     
        file = null;
    att  = null;
    }
    } catch (Exception e) {
        file = null;
        att  = null;
    }
    }
2
  • Please share your code . Commented Aug 11, 2014 at 14:21
  • edited to share some code cab add more if needed not sure what is relevant. Commented Aug 11, 2014 at 14:29

2 Answers 2

9

The only way to avoid this error in pure Visualforce is to make the backing variable transient. This may have a side effect of requiring the user to select the file to upload again.

For this reason, I usually suggest that file inputs not be used in complex forms, when possible. I usually make a form with the dedicated purpose of saving a file. This usually means making a multi-step wizard.

1
  • Thanks for the answer, sadly a multi-step wizard cant be used however helpful I would find it. Also transient seems to have no effect on the issue so maybe my problem is elsewhere (the exceeded viewstate size not being the same as the file size also makes me think that) but if I upload a smaller file it works fine Im unsure. Commented Aug 11, 2014 at 14:35
4

After insert make the blob variable as null so that in viewstate of page that does not go back.If multiple attachment go for wizard as mentioned by SFDC Fox .

public void submit() { 
MapRequest__c cont=(MapRequest__c) std.getRecord();
insert cont;
if (file != null){ 
    Attachment att = new Attachment(); 
    att.Name = UploadedfileName; 
    att.Body = file;
    att.ParentId = cont.Id; 
    att.Description = 'File Uploaded'; 
    insert att;     
}
  file=null;
5
  • I had this at first and it works great after insertion but if the it fails validation then it is never inserted so it never gets nulled Commented Aug 11, 2014 at 14:36
  • In the catch block also make it null .Its as simple as that .Use try catch mechanism to nullify on failure too Commented Aug 11, 2014 at 14:37
  • Does the standard validation for required fields have a catch block that I can access to put that in? Commented Aug 11, 2014 at 14:41
  • yes you can .Use catch and just nullify blob in the same Commented Aug 11, 2014 at 14:42
  • I edited the submit function in my post to reflect those changes and Im still getting the viewstate error. Commented Aug 11, 2014 at 14:49

You must log in to answer this question.

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