Skip to content

Instantly share code, notes, and snippets.

@modestemax
Created March 31, 2022 11:59
Show Gist options
  • Save modestemax/817500a530e3e79c76d36859f90a966b to your computer and use it in GitHub Desktop.
Save modestemax/817500a530e3e79c76d36859f90a966b to your computer and use it in GitHub Desktop.
from lwc download sobject attachment salesforce apex ligtning web component
downloadFile(){
getPDF({
filter:this.seachFilter
})
.then(result => {
console.log(result);
//check if no err
if(!result.Error)
this.saveFile( result);
})
.catch(error => {
console.error('Error:', error);
});
}
saveFile(file) {
var link = document.createElement('a');
link.innerHTML = 'Download PDF file';
link.download = file.fileName;
link.href = 'data:application/octet-stream;base64,' + file.Link;
document.body.appendChild(link);
link.click();
}
@AuraEnabled
public static Map<String, Object> getAttachment(String contactId) {
Map<String, Object> mapReturn= new Map<String, Object>();
try {
Attachment a=[select Name, Body, BodyLength from Attachment where ParentId = : contactId LIMIT 1];
mapReturn.put('Link',EncodingUtil.base64Encode(a?.Body));
mapReturn.put('fileName',a?.name);
} catch (exception e) {
mapReturn.put('Error',true);
mapReturn.put('ErrorMsg',e.getMessage());
}finally {
return mapReturn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment