I have a download text content functionality in my visualforce page, on clicking which a text file(.txt format) will be downloaded. This file will be created dynamically with the data stored in a Text field of a custom object. Now text file is getting downloaded but as empty file. Here, I am using a JavaScript function(which in turn will call action function to assign file content) on Onclick event. I could notice that my JS function is called after file download. Could somebody please tell me where I am getting wrong?
Javascript fn:
function fnDownloadContent(ID, compID)
{
actDnldContent(ID, compID);
}
Visualforce code:
<apex:actionfunction name="actDnldContent" action="{!fetchContent}" reRender="" >
<apex:param name="Id" value="" assignTo="{!Id}" />
<apex:param name="compId" value="" assignTo="{!CompId}"/>
</apex:actionfunction>
<apex:outputLink value="data:text/plain;charset=utf-8;base64,{!getEncodedData}" html-download="description.txt" >
<a ref="javascript:fnDownloadContent('{!lstDet.ID}','{!lstDet.CompId}');">Download</a>
</apex:outputLink>
Apex Controller code:
public String strContent { get; set; }
public String getEncodedData {get; set;}
public void fetchContent()
{
//Will be assigning text content here
strContent = 'Hello';
getEncodedData = EncodingUtil.base64Encode(Blob.valueOf(strContent));
}
So far, I could get the required content in a popup window after clicking on download, but it is not downloading any files.