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
SidharthSidharth 

Blob value (Excel) - display/download link in VF Page

Hi,

I have a requirement to display/provide download link to the http blob response in visualforce page.
I am able to create a new attachment/document from blob value and provide a download link to that in vf page using {!URLFOR($Action.Attachment.Download,ReportName)}".
But i dont want to create a new attachemnt, just pass it from apex to vf page.
Any ideas ?

Thanks
Sid
Best Answer chosen by Sidharth
EnreecoEnreeco
You need a String getter on your class (say public String base64Value{get;Set;} ):
base64value = EncodingUtil.base64Encode(httpResponse.getBodyasBlob());
And the link should be (xlsx file):
<a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,{!base64Value}">Download Excel</a>
In case you are using a xls file:

<a href="data:application/vnd.ms-excel;content-disposition:attachment;base64,{!base64Value}">Download Excel</a>

Let e know


All Answers

EnreecoEnreeco
Try this:
<a href="data:application/pdf;content-disposition:attachment;base64,{!BASE64_ENDOCED_BLOB}">DOWNLOAD</a>

In your controller create a "BASE64_ENCODED_BLOB" String getter in which you can encode the binary in Base64 (utf8).
When the user clicks on the link opens up the pdf in the tab (tested with Chrome, FF and Safari on MacOS)
Hope this helps
--
May the Force.com be with you
SidharthSidharth
Thanks for the reply.

1. The blob value i am getting from http response, is actually a xlsx file, which i can store it as an attachment using httpResponse.getBodyasBlob().

Will the suggested pdf solution will work in case of xlsx too ?
Can you provide a code snippet of encoding the binary in Base64 (utf8) ?

Thanks again!
EnreecoEnreeco
You need a String getter on your class (say public String base64Value{get;Set;} ):
base64value = EncodingUtil.base64Encode(httpResponse.getBodyasBlob());
And the link should be (xlsx file):
<a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,{!base64Value}">Download Excel</a>
In case you are using a xls file:

<a href="data:application/vnd.ms-excel;content-disposition:attachment;base64,{!base64Value}">Download Excel</a>

Let e know


This was selected as the best answer
SidharthSidharth
Thanks a lot , it worked awesome.
Only thing is do we have control over naming the downloaded file, as it seems to be defualt to download.xlsx.
EnreecoEnreeco
This is the only thing you cannot control (there is the "filename" attribute but it doesn't work)
SidharthSidharth
Cool, thanks !!
Gsekar DuraiGsekar Durai
ForceLogic,

Not working in Internet Explorer.. Is there any solution for this Please.
SidharthSidharth
Hi I have another requirement to directly download the excel file on sf custom button click.

Before: we were opening a vf page, which has a download link, click that will download the file
<a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,{!CustomCreditReport}">DOWNLOAD</a> 

Now: i am cutting down the steps, and want to directly download the file on button click
window.open('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,customReport');
But this is downloading a empty file. I can alert customReport and it has the data.

Is this possible ? Thanks.
Sid
PreeSFDCDevPreeSFDCDev
Hi Siddharth, Can you please explain the steps you did to dowload the file from vf page.
I have similar requirment - There are some search filters where user can select the search criteria and once he click on dowmload an xlsx file should create and dowload on user system.
 
SidharthSidharth
Preety,

In my case, we are getting a file content by calling an external partner web service, which i am encoding (EncodingUtil.base64Encode), and passing to vf page as string.

Apex:
String customCreditReport = EncodingUtil.base64Encode(httpResponse.getBodyasBlob());
(where, httpResponse is the external web service response)

VF Page:
<apex:pageBlock>
<a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,{!CustomCreditReport}">DOWNLOAD</a>
</apex:pageBlock>

-Sid
 
NHKNHK
Thank you Enreeco,

I was looking for similar solution but for PDF. i am able to display the blob as PDF in VF page with the following code.

<a href="data:application/pdf;content-disposition:attachment;base64,{!pdfString}">View File</a>

Best,
NHK
NHKNHK
@Enreeco,

The solution is working in Chrome and Firefox but not in IE 11(displying a blank white screen). Can you please suggest how can i make it to work in IE as well?

Best,
NHK
Jatin Bansal 14Jatin Bansal 14
@Enreeco, @Sidharth,

You can use download attribute for renaming the file and download it with custom name. You can use something like:
<a download='MyCustomName' href="data:application/pdf;content-disposition:attachment;base64,{!BASE64_ENDOCED_BLOB}">DOWNLOAD</a>
Clicking this will download file with name as 'MyCustomName.pdf'.

Hope this helps!