Question
Last activity: 15 May 2018 8:59 EDT
download file from a server location to local location on system
There is requirement , we have to save data from table to server location in .csv file and then to show that filename as a link in UI in Pega , on click of that link we have to download that file to local location on our system .
Hi zubins57,
To create a csv file and save it in the server location you can use following steps.
1. Create the header of the csv using a data transform.
e.g. Param.Header = "Country"+"\t"+"City" +"\t"+"Zip"+"\r\n"
2. Get the data into a pagelist and while looping through that set the data.
e.g. Param.Data = Param.Data+.Country+"\t"+ ".City"+"\t"+".Zip"+"\t"+"\r\n"
3. Concatenate header and data as the data source.
e.g. Param.DataSource = Param.Header + Param.Data
4. Create a Connect File Rule to save a file in the server.
5. Configure Destination path and File name to get from a page property.
e.g Destination Path value as =ConnectPage.FileLocation
6. Create a data transform to set the ConnectPage properties such as .FileLocation, .FileName
e.g. FileName = worldcities.xls
FileLocation = @Utilities.getDataSystemSetting(Ruleset,FileLocation)
In the DSS provide a directory in your server. E.g. /yourdirectory0/yourdirectory1/yourdirectory2
7. Create an activity to call the above data transforms and Connect-File rule.
8. In the Connect-File method configure operation as write from clipboard property and Parameter as Param.DataSource.
9. Call the activity from your desired action and check the server location for the generated file.
To download a file from the server location you can use following steps.
- Create an activity,
Step1 - configure parameters for the created FileName and FilePath.
E.g. Param.FilePath = Param.FileLocation + “/” + Param.FileName
Step 2 – Write java to get the file as output.
E.g.
String filePath=tools.getParamValue("FilePath");
String fileName=tools.getParamValue("FileName");
try
{
java.io.File fileIn=new java.io.File(filePath);
java.io.FileInputStream streamIn=new java.io.FileInputStream(fileIn);
PRFile file=new PRFile(fileName);
PROutputStream prOut=new PROutputStream(file,true);
int a;
while((a=streamIn.read())!=-1)
{
prOut.write(a);
}
streamIn.close();
tools.sendFile(file,true,null,true);
}
catch(Exception e)
{
oLog.error("Error when retrieving the file:"+e);
}
- Create a control rule and add a JS function as below example.
E.g.
<script>
function OpenFileFromServer()
{
var fileurl=new SafeURL("YourClassName.AboveCreatedActivityName");
window.open(fileurl.toURL(),"_self");
}
</script>
- Create a section and add a link. Add an action to on click event and call Run Script and give the above function name. Upon clicking on this link it should download your file from the server.


Pegasystems Inc.
IN
Hi,
Kindly check requirement in below PDN discussion similar to your requirement
https://pdn.pega.com/community/product-support/question/how-download-file-server-using-anchor-links
Anybody who can help ?? I would appreciate the effort for your answers.