Hi,
I'd like to have WebWork action return a file. Here's my atlassian.xml:
<action name="export" class="com.playsql.requirement.RequirementAction" method="export"> <result name="download" type="stream">inputStream</result> </action>
Here's my action:
public String export() { try { File file = File.createTempFile("export-", ".csv"); FileUtils.writeStringToFile(file, "bleblebleleheldbelhebelbelbele"); inputStream = new FileInputStream(file); return "download"; } catch (IOException e) { return "error"; } }
However, the browser just shows it, it doesn't download it and doesn't give it a name.
Using this question, I have tried:
<result name="download1" type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> <param name="contentDisposition">filename=document.csv</param> </result>
but I get this exception, which means WebWork doesn't detect that the contents of the <result> element isn't just text and it tries to parse it as if it were the location.
java.lang.NullPointerException at java.util.regex.Matcher.getTextLength(Matcher.java:1140) at java.util.regex.Matcher.reset(Matcher.java:291) at java.util.regex.Matcher.<init>(Matcher.java:211) at java.util.regex.Pattern.matcher(Pattern.java:888) at com.opensymphony.xwork.util.TextParseUtil.translateVariables(TextParseUtil.java:32) at com.opensymphony.webwork.dispatcher.WebWorkResultSupport.execute(WebWorkResultSupport.java:113) at com.opensymphony.xwork.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:263)
Community moderators have prevented the ability to post new answers.
Note: The other example won't work in IE, because it won't give it the right name. Instead:
public class RequirementAction extends AbstractSpaceAction implements ServletResponseAware { private HttpServletResponse response; public void setServletResponse(HttpServletResponse httpServletResponse) { this.response = httpServletResponse; } ... response.setHeader("Content-Disposition", "attachment; filename=" + "\"requirement-export.csv\""); response.setHeader("Cache-Control", "max-age=0"); response.setHeader("Pragma", ""); response.setContentType("text/csv"); OutputStream out = response.getOutputStream(); IOUtils.copy(inputStream, out); out.close();
Atlassian Answers is an awesome rubber duck in the sense it helps the community too!
<action name="export" class="com.playsql.requirement.RequirementAction" method="export"> <result name="download" type="stream"> <param name="contentType">text/csv</param> <param name="location">inputStream</param> <!-- Here's where I was mistaken --> <param name="contentDisposition">attachment; filename=requirement-export.csv</param> </result> <result name="error" type="velocity">/error.vm</result> </action>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Adrein,
The content disposition header is not at all reflecting in user's downloaded file.. Did it work for you?
Here is my configuration:
<result name="download" type="stream"> <param name="contentType">application/vnd.openxmlformats-officedocument.wordprocessingml.document</param> <param name="contentDisposition">attachment; filename=test.docx.html</param> <param name="inputName">inputStream</param> </result>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.