Created
March 31, 2022 11:59
-
-
Save modestemax/817500a530e3e79c76d36859f90a966b to your computer and use it in GitHub Desktop.
from lwc download sobject attachment salesforce apex ligtning web component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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