Saturday, November 4, 2017

To Create File using Connect File

To write a txt/xml file to Service Export directory:

In this post, we will see how to create text file using connect file rule in the "\StaticContent\global\ServiceExport" folder

Below are the steps to follow:










Step1: get the string which need to be written in the file. in our case, small xml string and getting in the parameter.








Step2: In java step, getting the path of the temp folder in a string, which will be passed to the connect file.











try {
     explicitTmpDir  = tools.getSystemSettings().getFSSetting("initialization/explicittempdir", "" , true, false);
if (explicitTmpDir == null || explicitTmpDir.length() == 0) {
explicitTmpDir = "Not Found By Method";
}
}
catch (Exception setCNF) {
explicitTmpDir = "Not Found By Exception: " + setCNF.toString();
}

Step3: setting the temp path string in Test Page.








Step4: Calling the Connect File Method, and Connect File Rule need to be created as follows:








Connect File rule need to be created with the below parameter setting which we set the values in the above activity.



















Please try and let me know the comments if any issues are anything need to be modified.

Friday, November 3, 2017

To Create and Download the file from Service Export

To write a txt/xml file to Service Export and  directory:

In this post, we will see how to create text file using java code in the "\StaticContent\global\ServiceExport" directory.











In step -1: Page new to create a new page for holding the data to be written in the text file.
In step -2: setting the paramters for file name and then xml string or data.
In step -3 below java code need to be added to create text file, in this case XML file is being generated.

java.io.OutputStream outStream = null;
try {
//Whatever the file path is.
//String strFileName="Example_Pega12.xml";
String pathname = tools.findPage("pxProcess").getProperty("pxServiceExportPath").getStringValue() + PRFile.separatorChar + strFileName;
//java.io.File statText = new java.io.File("C:\\Softwares\\examplePega.txt");
PRFile file = new PRFile(pathname);
PROutputStream fileStream = new PROutputStream(file);
outStream = new java.io.BufferedOutputStream(fileStream);
  byte[] outBytes=StrData.getBytes();
//byte buffer2[] = new byte[4096];
outStream.write(outBytes);
outStream.flush();
outStream.close();
}catch (java.io.IOException ioEx) {
oLog.error("Error writing uploaded file to local storage", ioEx);
}

In step 4- @baseclass DownloadFile activity and passing the stringFile name as parameter which we created in step 3, that in turn will have java code to download the file to the browser.

You can try and let us know the comments.