How can I download binary stream content in Pega and decode the binary data as per a specific requirement?
Hi @Yuvaraj for downloading the binary stream content, have to create a function for the java code, where we need to pass the parameters like fileType, fileName, fileContent, persistFileToServiceExport as per your requirement. you can the use the below code for your reference, this code will be used to decode the binary stream data and will be send to the browser for download.
PublicAPI tools=ThreadContainer.get().getPublicAPI();
boolean isFileDownloaded = false;
ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();
if(fileName.trim().length() == 0){
fileName = “Sample”; // If fileName is not passed setting it to sample
}
if(fileType.trim().length() ==0){
fileType = “pdf”; // If fileType is not passed setting it to pdf
}
if(fileContent != null && fileContent.trim().length()>0){
try{
byte pdfBytes = pdfoutStream.toByteArray();
byte byteStream = Base64Util.decodeToByteArray(fileContent); //decoding binary stream which was encoded
tools.sendFile(byteStream,fileName+“.”+fileType,persistFileToServiceExport, null,true); //sending stream content to browser
if(isFileDownloaded){
tools.sendFile(pdfBytes,fileName,false, null,isFileDownloaded);
}else{
tools.getParameterPage().putObject(“pdfByteArrayObj”,byteStream);
}
}catch(Exception ex){
pega_rules_default.pxLogMessage(“Exception while downloading the engine. Details are:”+ex);
return isFileDownloaded;
}
}
return isFileDownloaded;