I know attachments can insert through connect API. What about Files ? I have heapsize issues while insert an document with size of 5 mb

Here is My Controller to Insert a file

public static Id saveChunk(Id parentId, String fileName, String base64Data, String contentType, String fileId) {
    if (fileId == '') {
        fileId = saveTheFile(parentId, fileName, base64Data, contentType);
    } else {
        appendToFile(fileId, base64Data);

    return Id.valueOf(fileId);

public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {
    base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');

    ContentVersion cVersion = new ContentVersion();
    cVersion.PathOnClient = fileName;
    cVersion.Title = fileName;
    cVersion.IsMajorVersion = false;
    cVersion.VersionData = EncodingUtil.base64Decode(base64Data);
    Insert cVersion;
    Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;
    system.debug('con doc'+conDocument);
    ContentDocumentLink cDocLink = new ContentDocumentLink();
    cDocLink.ContentDocumentId = conDocument;
    cDocLink.LinkedEntityId = parentId;
    cDocLink.ShareType= 'V';

    Insert cDocLink;
    system.debug('cDocLink doc'+cDocLink); 
    return cVersion.id;

private static void appendToFile(Id fileId, String base64Data) {

    base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
    ContentVersion a = [SELECT VersionData,ContentDocumentId FROM ContentVersion WHERE Id =:fileId];
    String existingBody = EncodingUtil.base64Encode(a.VersionData);
    a.VersionData = EncodingUtil.base64Decode(existingBody + base64Data);

    update a; 
  • Kindly edit your question and add what you have tried so far? – Raul Nov 7 '19 at 7:57
  • @Raul Take a look – Gowtham sakthi Nov 7 '19 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.