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"
onchange="{!c.handleFilesChange}"
type="file"
name="file"
messageWhenTypeMismatch="Please upload supported file types"
accept=".jpg, .pdf, .jpeg, .png"
multiple="true"/>
<button class="slds-button slds-button_neutral" onclick="{!c.doSave}">Upload Files</button>
Controller.js:
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);
});
objFileReader.readAsDataURL(file);
}
}
This thing works for one file. What I want is to do a callback and pass all the files to handle them on apex.
Best Answer
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: