I am looking for bulk download functionality but not able to do it, please help me. I am trying to download records, if I select all the records from checkboxes.
Here I am using handleMassDownload for downloading records in below. I need to download all the files from table in zip format. Please help me out on above problem statment.
Here is my code:
<table class="slds-table slds-table_cell-buffer slds-table_bordered" aria-label="Example default base table of Opportunities">
<thead>
<tr class="slds-line-height_reset">
<th class="" scope="col">
<!---->
<lightning:input type="checkbox" aura:id="headerCheckbox" checked="{!v.isHeaderChecked}" name="headerCheckbox" onchange="{!c.headerCheckHandler}"/>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Decision">Decision</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Title">Title</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Type">Type</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Upload Date">Upload Date</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Owner">Owner</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Actions">Actions</div>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.documentList}" var="doc" indexVar="indx">
<tr class="slds-hint-parent">
<td class="slds-truncate">
<lightning:input type="checkbox" name="{!indx}" value="{!doc.Id}" aura:id="inputCheckbox" onchange="{!c.handleCheckboxChange}"/>
</td>
<td class="slds-truncate">
<lightning:icon iconName="utility:success" size="small" />
<aura:set attribute="else">
<aura:if isTrue="{!doc.Decision__c == 'Rejected'}">
<lightning:icon iconName="utility:error" size="small" />
<aura:set attribute="else">
<lightning:icon iconName="utility:warning" size="small" />
</aura:set>
</aura:if>
</aura:set>
</aura:if>-->
</td>
<td class="slds-truncate">
<a href="javascript:void(0);" onclick="{!c.navigateToAppPage}" data-record-id="{!doc.Id}">{!doc.Name}</a>
</td>
<td class="slds-truncate">{!doc.DocumentType__c}</td>
<td class="slds-truncate">{!doc.Upload_Date__c}</td>
<td class="slds-truncate">{!doc.Owner.Name}</td>
<td class="slds-truncate" >
<lightning:button label="Delete" iconName="utility:delete" onclick="{!c.handleDelete}" variant="destructive" value="{!doc.Id}" />
<lightning:button label="Download" iconName="utility:download" onclick="{!c.handleDownload}" value="{!doc.Id}" />
</td>
</tr>
</aura:iteration>
</tbody>
</table>
({
doInit: function(component, event, helper) {
helper.refreshDocumentList(component);
},
handleMassDownload : function(component, event, helper){
let navService = component.find( "navService");
let documentList = component.get('v.documentList');
let docId = event.getSource().get('v.value');
console.log('docId: ' + docId);
let cvId = '';
console.log('documentList: ', documentList);
documentList.forEach(function(item) {
console.log('item.Id: ', item.Id);
if (item.Id == docId) {
console.log('IF Match found!');
cvId = item.ContentVersionId__c;
}
});
console.log('cvId: ' + cvId);
let filesDownloadUrl = '/sfc/servlet.shepherd/version/download/' + cvId;
console.log('filesDownloadUrl : ' + filesDownloadUrl);
let pageReference = {
type: 'standard__webPage',
attributes: {
url: filesDownloadUrl
}
};
navService.navigate(
pageReference
);
},
headerCheckHandler: function(component, event, helper) {
var headerCheckbox = component.find("headerCheckbox");
var checkboxes = component.find("inputCheckbox");
if (!Array.isArray(checkboxes)) {
checkboxes.set('v.checked', headerCheckbox.get('v.checked'));
} else {
checkboxes.forEach(function(checkbox) {
checkbox.set('v.checked', headerCheckbox.get('v.checked'));
});
}
},
handleCheckboxChange: function(component, event, helper){
let selectedDocId = [];
let count = 0;
// let name = event.getSource().get('v.name');
let findAllInput = component.find('inputCheckbox');
component.set('v.isHeaderChecked',false);
console.log('findAllInput : ' + findAllInput.length);
//console.log('name : ' + name);
//let isChecked = findAllInput[name].get('v.checked');
for(let i=0; i<findAllInput.length; i++){
let isChecked = findAllInput[i].get('v.checked');
if(isChecked){
count++;
selectedDocId.push(findAllInput[i].get('v.value'));
}
}
/*if(count == findAllInput.length){
component.set('v.isHeaderChecked',true);
} else {
component.set('v.isHeaderChecked',false);
}*/
component.set('v.isHeaderChecked', count === findAllInput.length);
component.set('v.selectedDocId', selectedDocId);
console.log('selectedDocId : ' + selectedDocId);
},
handleDownload: function(component, event, helper) {
let navService = component.find( "navService");
let documentList = component.get('v.documentList');
let docId = event.getSource().get('v.value');
console.log('docId: ' + docId);
let cvId = '';
console.log('documentList: ', documentList);
documentList.forEach(function(item) {
console.log('item.Id: ', item.Id);
if (item.Id == docId) {
console.log('IF Match found!');
cvId = item.ContentVersionId__c;
}
});
console.log('cvId: ' + cvId);
let filesDownloadUrl = '/sfc/servlet.shepherd/version/download/' + cvId;
console.log('filesDownloadUrl : ' + filesDownloadUrl);
let pageReference = {
type: 'standard__webPage',
attributes: {
url: filesDownloadUrl
}
};
navService.navigate(
pageReference
);
},
handleFileChange: function(component, event, helper) {
component.set('v.isLoading',true);
let selectedFiles = component.find("fileId").get("v.files");
let selectedFile = selectedFiles[ 0 ];
console.log('File Name is',selectedFile.name);
component.set("v.fileName",selectedFile.name);
console.log('File Size is',selectedFile.size);
let objFileReader = new FileReader();
objFileReader.onload = $A.getCallback( function() {
let fileContent = objFileReader.result;
let base64 = 'base64,';
let dataStart = fileContent.indexOf( base64 ) + base64.length;
fileContent = fileContent.substring( dataStart );
helper.processFileUpload( component, fileContent, selectedFile );
} );
objFileReader.readAsDataURL(
selectedFile
);
//helper.uploadFiles(component, files);
},
navigateToAppPage: function(component, event, helper) {
//alert('navigateToAppPage');
// Get the record Id from the data-record-id attribute
var recordId = event.currentTarget.dataset.recordId;
//alert('recordId'+recordId);
// Use the navigation service to navigate to the app page
var navService = component.find("navService");
var pageReference = {
type: "standard__app",
attributes: {
appTarget: "TEST_PAGE_2"
},
state: {
recordIds: recordId
}
};
navService.navigate(pageReference);
}
})