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
Karthick RajaKarthick Raja 

How to download file(text) in visualforce page

Hi folks,
       I have a visualforce page in which there is a button called download.
If I click that button then it will download all the account records as text file. For this use case how we can implement.
I Dont want to redirect to another visualforce page
Can someone suggest some ideas to implement.

VFP:
User-added image


Thanks in advance,
Karthick
pconpcon
You could probably just set your url of the download link to be data:  If you look here [1] you can see how they do it in Javascript and it would be a matter of simply converting that over to VF.

My guess at the VF page
 
<a href="data:text/plain;charset=utf-8;base64,{!encodedData}">Download</a>

Controller
String data = '';

public String getEncodedData() {
    return EncodingUtils.base64Encode(Blob.valueOf(data));
}

I'm encoding it as base64 to make the url safe and not having to escape anything.  But you can do it without encoding if you escape correctly.

[1] http://cwestblog.com/2014/10/21/javascript-creating-a-downloadable-file-in-the-browser/