Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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;
    }
    }
share|improve this question
    
Please share your code . –  Mohith Shrivastava Aug 11 at 14:21
    
edited to share some code cab add more if needed not sure what is relevant. –  user2070057 Aug 11 at 14:29

2 Answers 2

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.

share|improve this answer
    
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. –  user2070057 Aug 11 at 14:35

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;
share|improve this answer
    
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 –  user2070057 Aug 11 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 –  Mohith Shrivastava Aug 11 at 14:37
    
Does the standard validation for required fields have a catch block that I can access to put that in? –  user2070057 Aug 11 at 14:41
    
yes you can .Use catch and just nullify blob in the same –  Mohith Shrivastava Aug 11 at 14:42
    
I edited the submit function in my post to reflect those changes and Im still getting the viewstate error. –  user2070057 Aug 11 at 14:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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