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
VivoVivo 

Downloading a CSV through visualforce page

Hi,

 

I have a CSV generating page set up right now, that I run from another page when they click the generate button. It is opening up the page and starting the download, but the blank page that it downloads from is being opened and stays open. Is there a way to make the page automatically close itself once the download begins?

 

<apex:page controller="csvController" cache="true" 
contentType="text/csv#filename.csv" language="en-US">



{!headers}



</apex:page>

 

Avidev9Avidev9
how are you accessing the page ?
VivoVivo

Hi,

 

I am calling it via outputlink:

 <apex:outputLink value="{!placetogo}" style="text-decoration:none;" styleClass="btn" target="_blank">Create</apex:outputLink>

 

Where placetogo is the URL for the page:

placetogo=URL.getSalesforceBaseUrl().toExternalForm() + '/apex/generatespreadsheet?id='+sheet.Scenario__c;

 

Thanks.

Avidev9Avidev9
Try replacing the outputLink with an commandLink

<apex:commandLink value="My Link" action="{!generateCSV}"/>

public Pagereference generateCSV(){
return new Pagereference(placetogo);
}
VivoVivo

Hi,

I tried this and it seems to just change the current page to the blank one and start the download..

 

Thanks,

Vivek

Avidev9Avidev9
Its just an I dea how about adding an action to the page ?
This will call a method in controller that will bring back to the original page.

VivoVivo

Hm I tried to do this but everything I write in the <script></script> tags on this page gets written onto the CSV instead of acting as a function

Avidev9Avidev9
Dont use script try page action attribute