Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: DownloadAction is struts portlet.
I have written an Action class that extends DownloadAction and this writes my report into a spreadsheet (.xls file). This action successfully writes my report into an excelsheet at the path specified but instead of showing 'File Download' pop-up window with "Do you want to open or save this file?" option it renders the .xls file as text with lot of junk characters. Not able to figure out why its rendering it as text with junk. I have even set the content-type in DownloadAction class to application/vnd.ms-excel(check below). I also see that the file is successfully written into the path specified.
I have also added <mime-mapping> tag in my web.xml file.
Here is how my action class looks
**************************************************************************************************
DownloadMyReport.java
**************************************************************************************************
public class DownloadMyReport extends DownloadAction {
protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
String contentType = "application/vnd.ms-excel";
String Fname = (String) ReportBean.get("Fname");
String Lname = (String) ReportBean.get("Lname");
.
expFile = new File("C:\\Reporta\\MyReport.xls");
.
.
return new DownloadAction.FileStreamInfo(contentType, expFile);
}
*****************************************************************************************************
Web.xml
*****************************************************************************************************
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
******************************************************************************************************
********************************************************************************************************
InputForm.jsp
*********************************************************************************************************
<form name="myReport" action="" method="post">
<input type="text" name="Fname">
<input type="text" name="Lname">
<a href="#" onclick="submitReport()">Submit Report</a>
</form>
<script type="text/javascript">
function submitReport() {
var resultWindow = window.open('<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
<portlet:param name="_spage" value="/portlet_action/Reporting_portlet/Reporting/DownloadMyReport"/>"/>
</portlet:actionURL>');
resultWindow .opener = window;
resultWindow .focus();
}
</script>
*********************************************************************************************************
struts-config.xml
*********************************************************************************************************
<form-beans>
<form-bean name="ReportBean" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="Fname" type="java.lang.String"/>
<form-property name="Lname" type="java.lang.String"/>
</form-bean>
<action path="/Reporting_portlet/Reporting/DownloadMyReport"
type="com.reporting.MyReport" name="ReportBean" scope="session" validate="true"
input="portlet.Reporting_portlet.ReportRequest"/>
**********************************************************************************************************
I have also added <mime-mapping> tag in my web.xml file.
Here is how my action class looks
**************************************************************************************************
DownloadMyReport.java
**************************************************************************************************
public class DownloadMyReport extends DownloadAction {
protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
String contentType = "application/vnd.ms-excel";
String Fname = (String) ReportBean.get("Fname");
String Lname = (String) ReportBean.get("Lname");
.
expFile = new File("C:\\Reporta\\MyReport.xls");
.
.
return new DownloadAction.FileStreamInfo(contentType, expFile);
}
*****************************************************************************************************
Web.xml
*****************************************************************************************************
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
******************************************************************************************************
********************************************************************************************************
InputForm.jsp
*********************************************************************************************************
<form name="myReport" action="" method="post">
<input type="text" name="Fname">
<input type="text" name="Lname">
<a href="#" onclick="submitReport()">Submit Report</a>
</form>
<script type="text/javascript">
function submitReport() {
var resultWindow = window.open('<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
<portlet:param name="_spage" value="/portlet_action/Reporting_portlet/Reporting/DownloadMyReport"/>"/>
</portlet:actionURL>');
resultWindow .opener = window;
resultWindow .focus();
}
</script>
*********************************************************************************************************
struts-config.xml
*********************************************************************************************************
<form-beans>
<form-bean name="ReportBean" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="Fname" type="java.lang.String"/>
<form-property name="Lname" type="java.lang.String"/>
</form-bean>
<action path="/Reporting_portlet/Reporting/DownloadMyReport"
type="com.reporting.MyReport" name="ReportBean" scope="session" validate="true"
input="portlet.Reporting_portlet.ReportRequest"/>
**********************************************************************************************************
I still haven't resolved this issue. It's been long I have posted this thread and no inputs yet. Please, if anyone has any inputs, will be glad to try them out.
From the Struts Wiki:
response.setHeader(
"Content-Disposition",
"attachment; filename=" + expFile.getName());
return new DownloadAction.FileStreamInfo(contentType, expFile);
Hi Dang,
Thanks for you response. I did try as u suggested but the result is still the same. The excel file is shown in the pop-up window as text with junk characters. Is there anything else you can suggest.
Do you think there's something wrong in the way I submit my form to generate the excel report? Here is the submit Function
function submitReport() {
var resultWindow = window.open('<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
<portlet:param name="_spage" value="/portlet_action/Reporting_portlet/Reporting/DownloadMyReport"/>"/>
</portlet:actionURL>');
resultWindow .opener = window;
resultWindow .focus();
}
Thanks for you response. I did try as u suggested but the result is still the same. The excel file is shown in the pop-up window as text with junk characters. Is there anything else you can suggest.
Do you think there's something wrong in the way I submit my form to generate the excel report? Here is the submit Function
function submitReport() {
var resultWindow = window.open('<portlet:actionURL windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
<portlet:param name="_spage" value="/portlet_action/Reporting_portlet/Reporting/DownloadMyReport"/>"/>
</portlet:actionURL>');
resultWindow .opener = window;
resultWindow .focus();
}
Vivek N Kapse:
Hi Dang,
The excel file is shown in the pop-up window as text with junk characters.
This is behavior I would expect if the Content-Disposition header was not properly set. Use Firefox with LiveHttpHeaders and make sure that you see the Content-Disposition header set to 'attachment'. Also make sure that both "Content-Disposition" and "attachment" are spelled correctly.
If you do not see a Content-Disposition header at all (or you see two of them -- one specifying 'inline' and the other specifying 'attachment'), it's very possible that the Struts DownloadAction is not compatible with either Liferay or the Struts portlet bridge.
Ray Augé wrote a very detailed HOWTO describing ways to deliver files from a portlet, depending on which version of Liferay you are trying to use. I recommend trying one of those techniques to deliver your file, in the event that the Struts DownloadAction is not providing you with the desired behavior.