function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Madhusudan Singh 19Madhusudan Singh 19 

Not able to download blob object in browser

Hello,

I am integrating SharePoint with salesforce and trying to download the file from Sharepoint to the browser without storing it in Salesforce.
I am able to get file data from SharePoint using APEX HTTP Callout but don't know how can I push it to the browser.

Regards
Madhusudan Singh
Ashish Singh SFDCAshish Singh SFDC
Hi Madhusudan,

There are multiple ways to do it. Below is one approach.

Convert your blob into Base64
//Apex 
public class pdfDownloader{       
public String newblobMessage {get;set;}

//Method
public static void getPDF(){
//HTTp Request and Response Logic
responseBody = response.getBody();
Blob blobMessage = response.getBodyAsBlob();
newblobMessage = EncodingUtil.base64Encode(blobMessage);
}
}
 
<apex:page controller="pdfDownloader"   action="{!getPDF}">

    <script>
 
    function openInNewTab(newblobMessage) {
        var link = document.createElement('a');
        link.innerHTML = 'Download PDF file';
        link.download = 'file.pdf';
        link.href = 'data:application/octet-stream;base64,' + newblobMessage;
        document.body.appendChild(link);
    }
    
    function replaceUrlWithPDF(newblobMessage){
    	
      // Navigate to the Location.reload article
        // window.open("data:application/pdf;content-disposition:attachment;base64,"+newblobMessage);
        
        window.open("data:application/pdf;base64,"+newblobMessage);
        window.location.reload();
       
    }
    
    function base64ToArrayBuffer(data) {
        const bString = window.atob(data);
        const bLength = bString.length;
        const bytes = new Uint8Array(bLength);
        for (let i = 0; i < bLength; i++) {
            bytes[i] = bString.charCodeAt(i);
        }
        return bytes;
    }
    function base64toPDF(base64EncodedData, fileName = 'file') {
        const bufferArray = base64ToArrayBuffer(base64EncodedData);
        const blobStore = new Blob([bufferArray], { type: 'application/pdf' });
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(blobStore);
            return;
        }
        const data = window.URL.createObjectURL(blobStore);
        const link = document.createElement('a');
        document.body.appendChild(link);
        link.href = data;
        link.download = `${fileName}.pdf`;
        link.click();
        window.URL.revokeObjectURL(data);
        link.remove();
    }
        
    </script>
    
    
    <apex:form >
         <apex:outputLink onclick="replaceUrlWithPDF('{!newblobMessage}')">Download with JavaScript</apex:outputLink> 
        
<a target="_blank" href="data:application/pdf;content-disposition:attachment;base64,{!newblobMessage}" >Download with Command Button</a>

        <a href="data:application/octet-stream;base64, {!newblobMessage}">Download PDF</a>

        <apex:outputLink onclick="base64toPDF('{!newblobMessage}')">One More JS Way</apex:outputLink>
    </apex:form> 
    
</apex:page>

There are other ways as well:
1. https://help.salesforce.com/HTViewSolution?id=000044612
2. You can use https://github.com/MrRio/jsPDF Library with Lightning.

Thanks,
Ashish Singh.