Question
Virtusa
US
Last activity: 10 Jan 2017 5:28 EST
Unable to render External Attachment data in browser
Hi
I am creating this post as it was re-directed from the Pega SR(SR-A93915) request . Below are the primary details about the issue description .
Requirement :
To upload and retrieve documents from an external storage system (Azure Blob) Via Rest Service API
--- File Type : All ; ex: pdf , Excel , Image , txt , docx etc
Issue :
-- the raw data (without any encoding) response recieved from the rest service is not being rendered correctly in the browser.
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Virtusa IT Consulting
AE
can you please elaborate how you are handling the byte stream returned in response? are you mapping it to 'pyAttachStream'?
Virtusa
US
Hi ,
I am not assigning it to "pyStream" , for txt file i am using show-property in activity to display the contents of the REST service response in browser ,which opens up correctly , issue is with files other than type txt .
For other than txt file :
byte[] byteArray=(byte[])tools.getParameterPage().getParameterValue("PDFDocument");
HashStringMap aMap = new HashStringMap();
aMap.put( "ContentDisposition", "attachment;filename=\"aaa.pdf\"" );
aMap.put( "ContentType", "application/attachment" );
String result=tools.sendFile(byteArray,"aaa.pdf",false,aMap,true);
PDFDocument - contains the REST API response which is a string holding byte array data , so converting to bytearray viariable and sending it to the browser.
Incessant Technologies
IN
Pegasystems Inc.
SG
What do you mean by not rendering correctly in the browser? Do you mean that the attachment is not displayed in the browser like the text file is? Does it display a "save/download" option to the user?
If the above description is correct change the value for "ContentDisposition" to "inline" and "ContentType" to "application/pdf".
Without the first the browser defaults to save/download and without the second it can't determine what external viewer to use if it's unable to render the document itself.
As a caveat, remember ContentDisposition inherently is a security risk as outlined in RFC 6266.
Regards
Patrick
Virtusa
US
Hi Patrick ,
Below is the screen on how document was displayed in browser .
Thanks.
Below screen is When i try to display the response proeprty with out any alteration - using show-property
Pegasystems Inc.
SG
Not sure I understand. Show-Property wasn't meant to do what you are trying to do. SendFile API sends the file to the browser directly. Storing the output of the API and displaying it with show-property won't work. The appropriate headers needed for this to work won't get sent. The response to your REST request should be to respond directly with the sendFile API.
Regards
Patrick
Virtusa
US
Hi Patrick ,
I tried with sendFile API also , but no luck , Apreciate your help . Thanks.
Code :
Output :
Pegasystems Inc.
GB
You should NOT have to set up the HTTP Headers map: you should let 'sendFile' build these automatically given the filename (including the file extension).
You just need to have a Byte Array loaded with the contents of your Binary File; and then pass this along with a filename to the 'sendFile' API.
Take a look at the OOTB Activity 'Code-Pega-PDF.View' to see how it is able to stream a PDF back to the browser (the flags govern whether the 'attachment-disposition' are set or not - 'Code-Pega-PDF.View' sets them so that the PDF is launched by an external viewer [usually] , rather than replacing the current content in the browser).
Pegasystems Inc.
SG
I suspect there are either one or both of the issues below. Try changing one at a time to see if it resolves the problem.
1) The ContentDisposition header name should be passed as Content_Disposition (Pega will replace the underscore with a hyphen at run time). Optionally, remove the filename parameter.
2) Replace decodeToByteArray with encodeToByteArray in your code.
Regards
Patrick
Virtusa
US
As suggested , Tried with the below code , still not able to successfully open the file in browser.
US
Hi Adithya,
We have a similar requirement. Were you able to acheive this?
Thanks,
Chaitanya