I'm working on a visualforce project that requires generating csv file from a group of field values.
I'm using the following code to generate csv, I'm supposed to click on a command button to generate the file but the problem is that when i click save on the apex page an empty file is generated without taking the required data.
Visualforce page:
<apex:page controller="test2" contentType="text/csv#{!fileName}.csv" showHeader="false" sidebar="false" standardStylesheets="false">
<apex:form >
<apex:outputText value="{!input}" escape="false"/>
<apex:commandButton action="{!exportContent}"/>
</apex:form>
</apex:page>
Controller:
public with sharing class test2 {
// Controller for ExportData.page,
// which expects to be handed the following POST variables:
// @inputdata(string) data to export (i.e. in CSV format)
// @filename(string) name of the file to export, e.g. 'AccountData'
public transient String input { public get; private set; }
public transient String fileName { public get; private set; }
public void exportContent(){
Map<String,String> params = ApexPages.currentPage().getParameters();
// We expect to be handed POST variables called 'inputdata' and 'filename'
fileName = params.get('filename');
if (fileName == null) fileName = 'Data';
input = params.get('inputdata');
if (input == null) input = 'No data provided.';
}
}