Question

Issues with launching the excel file using tools.sendFile API

I am having issues with launching the excel file using tools.sendFile API. I am using the tools.sendFile(buffer, "compare.xls", false, null, true) to launch an excel file.

I have verified the buffer content and its completely alright. In the fiddler the content type is coming as application/octet-stream. In another scenario where I was able to launch excel from non-pega url, the content type is coming as application/vnd.ms-excel.

Is there an issue with the way, i am using the tools.sendFile API? I cant see any error in log or something like that, but the file is not launched, nothing happens.

Code below

/*Creating byte Array by reading excel file*/
java.io.File objFile = new java.io.File(tools.getParamValue("filedest"));

java.io.DataInputStream dis = null;
byte buffer [] = null;
try
{
dis = new java.io.DataInputStream(new java.io.FileInputStream(objFile));
buffer= new byte[dis.available()];
dis.readFully(buffer);
dis.close();
dis = null;
}
catch (Exception e)
{
throw new PRRuntimeException("Can't retrieve file attachment. Can't read File",e);
}

finally
{
if (dis != null) {
try {
dis.close();
} catch (Exception ex) {}
}
}
tools.putParamValue("length", buffer.length);
/*calling sendFile API of Pega*/
String sErrorMessage = tools.sendFile(buffer, "compare.xls", false, null, true);
if (sErrorMessage != null){
tools.getThread().getThreadPage().putString("pyXMLStream", sErrorMessage);
pega.terminateActivity();}

***Updated by moderator: Lochan to close post***
This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Comments

Keep up to date on this post and subscribe to comments

June 29, 2015 - 1:18pm

Hi Chaithanya, try the following

StringMap stringMap = new HashStringMap();

stringMap.put("contentType", "application/vnd.ms-excel");

String sErrorMessage = tools.sendFile(buffer, "compare.xls", false, stringMap, true);

June 29, 2015 - 2:16pm
Response to GopiGanapathy

Hi Gopi,

Thanks for your answer, The content-Type is correctly coming as the "application/vnd.ms-excel" now.

 

In the fiddler the content type is rightly showing.

 

But my issue is still not completely resolved as the excel launch is not happening. I dont see the log file error message also.

 

In the fiddler, I can clearly see that the file content is sent to the client browser.

 

Can you suggest what might be the error in this scenario?

June 29, 2015 - 3:29pm

If you click "file - > open" from your browser and select the file, does it open ?  If not, then the Pega product is no longer involved in the issue (unless there's something in the Pega-generated content of the file that contributes to it not opening).  /Eric

June 29, 2015 - 3:35pm
Response to ericosman_GCS

Hi Eric,

I didnt quite understand when you say file-> open from browser.

Are you referring to the windows dialog box which opens when we are trying to add an attachment where we have to perform file -> open?

Yes, In that dialog I am able to add attachments to the Pega application.

June 29, 2015 - 3:37pm
Response to ericosman_GCS

Hi Eric,

Sorry, I got what you meant.

I did file -> open from browser and gave the full path of the file.

Yes, I am able to open the file through this way.

Pega
June 30, 2015 - 4:46am
Response to chaithanyas2395

Hi Chaithanya,

With regard to the secondary issue : where you cannot launch excel automatically from the clicking a link - I believe you might want to try setting the 'ContentDisposition' [ HTTP/1.1: Appendices ]

If you take a look at the OOTB Activity "ViewExcelData|Pega-WB:07-10-01" (I'm referencing a PRPC 7.1.7 System here) you'll see the following Java Step:

ClipboardPage objHeadersPage = tools.findPage("pxRequestor").getProperty(".pyHTTPResponseHeaders").getPageValue();
objHeadersPage.putString("contentType" ,"application/vnd.ms-excel");
objHeadersPage.putString("ContentDisposition" ,"attachment;filename=ExportData.xls");

In fact, you *might* be able to re-use this activity instead of re-inventing the wheel here ? (It's probably enough for you to ensure your EXCEL content is contained in  a PARAM called 'pyViewData' and just call this Activity ?)

Thanks,

John

June 30, 2015 - 11:44am
Response to JOHNPW_GCS

Hi John,

I am generating the excel using an external tool called ASPOSE for content formatting, color coding etc.

How can I bring the excel data into pyViewData param?

Regards,

Chaithanya

Pega
June 30, 2015 - 1:02pm
Response to chaithanyas2395

Hi Chaithanya,

Actually - I just checked this - and this approach will NOT work here I don't think - you are (probably?) dealing with a BINARY FILE here - and the Activity I previously suggested can only really be used for TEXT (actually HTML/XML) formats for EXCEL.

Sorry to lead you astray there !

Cheers

John

PS: despite not being able to use the OOTB Activity here - I would still advise that you might want to split out your Activity into two bits here - you can pass around Java Objects in the Parameter page; so you can read the byte[] array in one activity - and then (using 'Pass Current Parameter Page') call one from the other.

You can look at 'Code-Pega-PDF' for the idea here. (It just uses 'sendFile' as you are - it doesn't set any headers - as I believe sendFile is doing that automatically - but I would recommend a 'belt-and-braces' for your case - where you set the HTTP headers as you already doing and then call sendFile).

April 25, 2016 - 1:31pm
Response to JOHNPW_GCS

Any insight on how we can send binary code to the browser using mStreamResponse?

see also: Re: API for streaming bytes for the output response