+ Start a Discussion
NareshKrishnaNareshKrishna 

How can we download string as file in visualforce ?

Hi All,

 

How can we Download String variable defined in controller as text file using visualforce ?

 

Could anyone help me with this.

 

Thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You should be able to do this by having a page that contains the string and specifying the contentType attribute.  Something like:

 

<apex:page controller="MyController" contentType="text/plain/#test.txt"> 
   <apex:outputText value="{!myString}"/>
</apex:page>

 

All Answers

bob_buzzardbob_buzzard

You should be able to do this by having a page that contains the string and specifying the contentType attribute.  Something like:

 

<apex:page controller="MyController" contentType="text/plain/#test.txt"> 
   <apex:outputText value="{!myString}"/>
</apex:page>

 

This was selected as the best answer
Abhinav GuptaAbhinav Gupta

Bob's solution is correct, so accepting it as solution. 

NareshKrishnaNareshKrishna

Thanks bob :)

NareshKrishnaNareshKrishna

Hi,

 

In the same way, I tried to download image as png format.

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:pagecontentType="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.

bob_buzzardbob_buzzard

apex:image renders HTML.  You'll need to write the actual content of the image file as the body of that page.  I'm afraid that's not something I've done before.   How are you accessing this page though?  If its via a link you should be able to make the target of the link the image.

NareshKrishnaNareshKrishna

I am accessing page directly from broser:

 

https://c.ap1.visual.force.com/apex/DownloadExp

bob_buzzardbob_buzzard

But presumably you aren't just sitting down and typing that link in.  Are you clicking on a link from another page, or out of an email for example?