salesfoce 读取记录附件并下载 ContentDocumentLink ContentVersion

发布时间 2023-07-28 19:44:25作者: 赫赫332

由于项目自定义比较高,所以按照下面方式实现了,附件下载。

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 {
               
            }
        })
    }