Get file API
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".
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.
responseType parameter set to STREAMIf 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);
}
}
}
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 the pyContents 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);
}
Previous topic Create file API Next topic List files API