+ Start a Discussion
Naresh Krishna.ax1176Naresh Krishna.ax1176 

Download Image using apex



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

 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



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


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"  />

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"  />

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?




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


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"  />


could you please look into it.