• Breaking News

    Wednesday, 25 January 2017

    HOW TO UPLOAD LARGE FILE WHEN GETTING INPUT TOO LONG ERROR IN REMOTE ACTION


    In Salesforce when we upload file using remote action then we are not able upload file more then 4.3 MB because above 4,3 MB we getting input too long error.so I found one solution in which we are able to upload file upto 25 MB In this approach we are using sforce api on visual force page to upload file on salesforce.below is code of uploading file upto 25 MB.






    <apex:page >
        <script type="text/javascript">
             var __sfdcSessionId = '{!GETSESSIONID()}';
        </script>
        <script src="/soap/ajax/34.0/connection.js" type="text/javascript"></script>
        <script>
        function uploadAttachmentFromVisualForcePage(){
            var reader = new FileReader();
            var attachFile = document.getElementById('idOfUploadInputFileElement').files[0];
        
            if(attachFile == undefined){
                alert('Please select a file');
                return;
            }
        
            if(attachFile.size > 26214400){  //Where 26214400 is byte equivalent of 25MB
                alert('Attachment size not supported');
            }
        
            reader.onload = function(e) {
                var attachment = new sforce.SObject('Attachment');  
                attachment.Name = attachFile.name;
                attachment.IsPrivate = false;  
                attachment.ContentType = attachFile.type;
                attachment.Body = (new sforce.Base64Binary(e.target.result)).toString();;
                attachment.Description = attachFile.name;
                attachment.ParentId = 'recordID';   //Where recordID is the ID of record to which you want to add your attachment
                var result = sforce.connection.create([attachment]);  
                if(result[0].getBoolean("success")){  
                    alert('file uploaded successfully');
                }else{
                    alert('something went wrong.');
                }
            };
            reader.readAsBinaryString(attachFile);
        }    
        </script>
        <input type="file" id="idOfUploadInputFileElement" /><input type="button" value="Upload" onClick="uploadAttachmentFromVisualForcePage();"/>
    </apex:page>
    

    No comments:

    Post a Comment