|
Friday, 22 March 2013
Posted by Unknown on 04:17 with No comments
Posted by Unknown on 03:25 with No comments
As you know the viewstate is limited to 135KB so loading files larger than this size (if not done properly) will give you this error:
Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 395KB So how to write this code properly? |
< apex:page standardController = "Document" extensions = "TestFileUploadPageControllerExtension" > < apex:form > < apex:pageBlock title = "Attach file" mode = "edit" > < apex:pageBlockSection columns = "1" > < apex:pageBlockSectionItem > < apex:outputLabel value = "Select a file" for = "inputFile" /> < apex:inputFile id = "inputFile" value = "{!document.Body}" filename = "{!document.Name}" fileSize = "{!document.BodyLength}" contentType = "{!document.ContentType}" size = "40" /> </ apex:pageBlockSectionItem > </ apex:pageBlockSection > < apex:pageBlockButtons location = "top" > < apex:commandButton action = "{!doAttach}" value = "Attach" id = "attachButton" /> </ apex:pageBlockButtons > </ apex:pageBlock > </ apex:form > </ apex:page > |
public class TestFileUploadPageControllerExtension { //reference to standard controller private ApexPages.StandardController controller; public TestFileUploadPageControllerExtension(ApexPages.StandardController controller) { this .controller = controller; } public void doAttach() { System.Debug( '##################################' ); //get uploaded document Document doc = (Document) this .controller.getRecord(); System.Debug( 'doc: ' +doc); //create attachment from document Attachment a = new Attachment(); a.Body = doc.Body; a.ContentType = doc.ContentType; a.Name = doc.Name; a.ParentId = '0018000000OtgFl' ; //hard coded account id Database.SaveResult sr = Database.Insert(a); System.Debug( 'Attachment SaveResult: ' +sr); doc.body = null ; // Ensure that the blob is not included automatically in the serialized image of the standard controller. } } |
As noted in the code above, set the body of the new document is set to null to ensure that the blob is not included automatically in the serialized image of the standard controller.
|
Subscribe to:
Posts (Atom)