salesfoce 读取记录附件并下载 ContentDocumentLink ContentVersion
由于项目自定义比较高,所以按照下面方式实现了,附件下载。
apex :传入关联记录id
@AuraEnabled(cacheable=true)
public static Map<String, Object> getAttachmentInfo(String partnerOrderId){
Map<String, Object> resMap = new Map<String, Object>();
try {
resMap.put('status', 'success');
List<ContentDocumentLink> contentDL = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =:partnerOrderId];
List<String> contentDocumentIdList = new List<String>();
for (ContentDocumentLink contentRec : contentDL) {
contentDocumentIdList.add(contentRec.ContentDocumentId);
}
List<ContentVersion> datas = [select ContentDocumentId, VersionDataUrl,Title from ContentVersion where ContentDocumentId in:contentDocumentIdList AND IsLatest = true];
resMap.put('datas', datas);
} catch (Exception e) {
resMap.put('status', 'fail');
resMap.put('error', e.getMessage()+'\n'+e.getStackTraceString());
}
return resMap;
}
html:
<div for:each={attachmentList}
for:item="attachment"
for:index="index"
class="attachment-row"
key={attachment.ContentDocumentId}>
<div class="attachment-row-item">
<img src={attachmentGreyIcon} style="margin-right: 8px;"/>
{attachment.Title}
</div>
<div class="attachment-row-item attchment-action">
<img src={viewIcon} alt="" style="margin-right: 12px;">
<a class="download-herf">
<img src={downloadIcon} alt="" onclick={downloadAttachmentMethod} data-recid={attachment.ContentDocumentId}>
</a>
</div>
</div>
javascript:
@wire(getAttachmentInfo,{partnerOrderId:'$partnerOrderId'})
getAttachmentInfo(value){
const { data, error } = value; // destructure the provisioned value
if (data) {
if (data.status === "success") {
console.log("data.datas.length:"+data.datas.length);
this.attachmentList = data.datas;
} else {
this.showToast("error","",data.error);
}
}
else if (error) {
console.log('(error---> ' + JSON.stringify(error));
}
}
downloadAttachmentMethod(event){
var idexGet = event.target.getAttribute('data-recid');
getAttachmentSalesBussinessData({contentDocId:idexGet}).then(value => {
const data = value; // destructure the provisioned value
if (data.status === "success") {
var contentRec = data.datas;
var bstr = atob(contentRec.VersionData);
var n = bstr.length;
var u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
var blobRec = new Blob([u8arr], { type: contentRec.FileType });
var aherf = this.template.querySelector('.download-herf');
aherf.setAttribute("href",blobRec);
aherf.setAttribute("download",contentRec.Title);
aherf.setAttribute("target","_blank");
aherf.click();
} else {
}
})
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结