I want to upload multiple files from a public site to salesforce ContentDocumentLink. How I can create bulk of files on javascript (client) side and handle them from apex?

Lightning Component:

    <lightning:input aura:id="fileId" 
                                                 messageWhenTypeMismatch="Please upload supported file types"
                                                 accept=".jpg, .pdf, .jpeg, .png" 
<button class="slds-button slds-button_neutral" onclick="{!c.doSave}">Upload Files</button>


doSave: function(component, event, helper) {
        var fileCmp = component.find("fileId");
        if (!$A.util.isUndefined(fileCmp)) {
            if (fileCmp.get("v.files").length > 0) {

            // get the selected files using aura:id [return array of files]
             var fileInput = component.find("fileId").get("v.files");
             // get the first file using array index[0]  
             var file = fileInput[0];
             var self = this;

                   // create a FileReader object 
            let objFileReader = new FileReader();
            // set onload function of FileReader object   
            objFileReader.onload = $A.getCallback(function() {
                var fileContents = objFileReader.result;
                var base64 = 'base64,';
                var dataStart = fileContents.indexOf(base64) + base64.length;

                fileContents = fileContents.substring(dataStart);
                // call the uploadProcess method 
                self.uploadProcess(component, file, fileContents);


This thing works for one file. What I want is to do a callback and pass all the files to handle them on apex.

1 Answer 1


You need to iterate the selected files in the doSave method. Look into this link for more reference:

For multi-file upload same file being uploaded twice

After you read the contents of each file using the FileReader Object, you can pass them over to Apex and create a ContentVersion and ContentDocumentLink record like this:

public static void uploadFile(String fileName, String base64Data, String contentType) { 
    base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
    ContentVersion contentVersion = new ContentVersion(
        versionData = EncodingUtil.base64Decode(base64Data),
        title = fileName,
        pathOnClient =  '/' + fileName);
    insert contentVersion;

    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = contentVersion.ContentDocumentId;
    cdl.ShareType = 'C';
    insert cdl;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .