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
Naresh Krishna.ax1176Naresh Krishna.ax1176 

Download Image using apex

Hi,

 

I need to download an image using apex code when click on 'Download' link.

For this below is the code

<apex:page controller="TestController">
<apex:form >
<apex:commandLink action="/apex/DownloadImage"
style="font-size:15px;color:#0066CC;text-decoration:none;font-family:Segoe UI,Arial,Helvetica,sans-serif;" >
Download Image
</apex:commandLink>
</apex:form>
</apex:page>

 and in the 'DownloadImage'  <apex:page controller="TestController" contentType="I/MIME#image.png"> also tried with the contentType="image/png#image.png". With these content types able to download 'image.png' but the content in the file is not in '.png' format.

 

I am able to download a '.crt' file with the contentType="S/MIME#certificate.crt" and it is working fine.

 

Can anyone please help me on downloading an image using apex code.

 

Thanks in advance.

Edwin VijayEdwin Vijay

Hello,

 

So, are you trying to download an image hosted somewhere in the web or uploaded within Salesforce... Either case, i don't see a necessity to use the contenttype attribute

 

Maybe, i am missing something

NareshKrishnaNareshKrishna

Code used to download image as png:

<apex:page contentType="image/png#samplelogo.png" >
<apex:image url="http://ap1.salesforce.com/img/seasonLogos/sales_logo_aloha_spr12.png"  />
</apex:page>

With above code able to download file in png format but image not available in the downloaded file.

 

Code used to download image as html:

<apex:page contentType="text/html#samplelogo.html">
<apex:image url="https://ap1.salesforce.com/img/seasonLogos/sales_logo_aloha_spr12.png"  />
</apex:page>

With above I can able to download and view the image.

 

But I need to download the image as png or jpeg format.

 

Could you please help me with that?

 

Thanks

cloudmaniacloudmania

use "application/octet-stream"  instead "image/png" in contenty type.

NareshKrishnaNareshKrishna

Thanks for your reply:

 

I tried below but not working:

<apex:page contentType="application/octet-stream##chart.png" >
<apex:image url="http://ap1.salesforce.com/img/seasonLogos/sales_logo_aloha_spr12.png"  />
</apex:page>

 

could you please look into it.