Skip to main content


         This documentation site is for previous versions. Visit our new documentation site for current releases.      
 

This content has been archived and is no longer being updated.

Links may not function; however, this content may be relevant to outdated versions of the product.

Get file API

Updated on April 5, 2022

The get file API is a read-only data page, D_pxGetFile. This API loads content or a stream to a file when the data page is loaded.

To use this API, define the following parameters:

  • repositoryName – the name of the repository from which to get the file.
  • filePath – the path, including the name of the file, to be retrieved, relative to the root path of the repository.
  • responseType – the type of content that is in the file to be retrieved, either "STREAM" or "STRING".

Note: It is a best practice to use the Stream responseType. Use the String option only for backward compatibility purposes.

Using the file content that the get file API returns to the external application

Consider a use case in which you use the D_pxGetFile data page in an activity to source a file that is then sent back to the browser to be downloaded to local disk storage.

reponseType parameter set to STREAM

If the D_pxGetFile data page is invoked with the responseType parameter set to STREAM, the file content is displayed on the data page as a java.io.InputStream object that is the value of the pyStream property.

The following example shows the java step in which the data page is given as the step page:

// Get the file contents from the data page as a Base64-encoded string value
                                Object fileStream = myStepPage.getObject("pyStream");
                                if (fileStream != null && fileStream instanceof java.io.InputStream) {
                                try {
                                // Send the file data back to the browser
                                // NOTE: fileName is a local variable of type String that has been set to the name of the file being downloaded
                                tools.sendFile((java.io.InputStream)fileStream,fileName,null,true);
                                }
                                finally {
                                // Always close the input stream
                                try {
                                ((java.io.InputStream)fileStream).close();
                                }
                                catch (java.io.IOException e) {
                                oLog.error("caught exception while closing input stream", e);
                                }
                                }
                                }
                            

Note: The application that is calling this data page must close the stream object after downloading the file data. If the application leaves the stream object open, subsequent calls to the data page might fail because all of the available HTTP connections to the repository are left open.
reponseType parameter set to STRING

If the D_pxGetFile data page is invoked with the responseType parameter set to STRING, the file content is displayed on the data page as a Base64-encoded string that is the value of thepyContents property.

The following example shows the java step that sends the file data to the browser and has the data page given as the step page:

// Get the file contents from the data page as a Base64-encoded string value
                        String fileContents = myStepPage.getString("pyContents");
                        if (fileContents != null && fileContents.length() > 0) {
                        // Decode the Base64 string to a byte array
                        byte[] fileBytes = java.util.Base64.getDecoder().decode(fileContents);
                        // Send the file data back to the browser
                        // NOTE: fileName is a local variable of type String that has been set to the name of the file being downloaded
                        tools.sendFile(fileBytes,fileName,false,null,true);
                        }
                    

Have a question? Get answers now.

Visit the Support Center to ask questions, engage in discussions, share ideas, and help others.

Did you find this content helpful?

Want to help us improve this content?

We'd prefer it if you saw us at our best.

Pega.com is not optimized for Internet Explorer. For the optimal experience, please use:

Close Deprecation Notice
Contact us