Friday, 24 March 2017

How upload large file in salesforce

Arpit Sharma


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.

  1. <apex:page>
  2. <script type="text/javascript">
  3. var __sfdcSessionId = '{!GETSESSIONID()}';
  4. </script>
  5. <script src="/soap/ajax/34.0/connection.js" type="text/javascript"></script>
  6. <script>
  7. function uploadAttachmentFromVisualForcePage() {
  8. var reader = new FileReader();
  9. var attachFile = document.getElementById('idOfUploadInputFileElement').files[0];
  10. if (attachFile == undefined) {
  11. alert('Please select a file');
  12. return;
  13. }
  14. if (attachFile.size > 26214400) { //Where 26214400 is byte equivalent of 25MB
  15. alert('Attachment size not supported');
  16. }
  17. reader.onload = function(e) {
  18. var attachment = new sforce.SObject('Attachment');
  19. attachment.Name = attachFile.name;
  20. attachment.IsPrivate = false;
  21. attachment.ContentType = attachFile.type;
  22. attachment.Body = (new sforce.Base64Binary(e.target.result)).toString();;
  23. attachment.Description = attachFile.name;
  24. attachment.ParentId = 'recordID'; //Where recordID is the ID of record to which you want to add your attachment
  25. var result = sforce.connection.create([attachment]);
  26. if (result[0].getBoolean("success")) {
  27. alert('file uploaded successfully');
  28. } else {
  29. alert('something went wrong.');
  30. }
  31. };
  32. reader.readAsBinaryString(attachFile);
  33. }
  34. </script>
  35. <input type="file" id="idOfUploadInputFileElement" /><input type="button" value="Upload" onClick="uploadAttachmentFromVisualForcePage();" />
  36. </apex:page>

About the Author

Arpit Sharma / Author & Editor

Certified Salesforce Developer | Salesforce Developer at Cognizant | Blogger