Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to return a file from an Action?

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.&lt;init&gt;(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)

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted

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!

&lt;action name="export" class="com.playsql.requirement.RequirementAction" method="export"&gt;
    &lt;result name="download" type="stream"&gt;
        &lt;param name="contentType"&gt;text/csv&lt;/param&gt;
        &lt;param name="location"&gt;inputStream&lt;/param&gt; &lt;!-- Here's where I was mistaken --&gt;
        &lt;param name="contentDisposition"&gt;attachment; filename=requirement-export.csv&lt;/param&gt;
    &lt;/result&gt;
    &lt;result name="error" type="velocity"&gt;/error.vm&lt;/result&gt;
&lt;/action&gt;

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:

&lt;result name="download" type="stream"&gt;
                    &lt;param name="contentType"&gt;application/vnd.openxmlformats-officedocument.wordprocessingml.document&lt;/param&gt;
                    &lt;param name="contentDisposition"&gt;attachment; filename=test.docx.html&lt;/param&gt;
                    &lt;param name="inputName"&gt;inputStream&lt;/param&gt;
                &lt;/result&gt;
TAGS
Community showcase
Published in Confluence

Wouldn't it be nice to have this Jira-Confluence integration?

...ossibility became true in a not too far future, project/space access would be left in the hands of all team members. Don't get me wrong: I'd love it! However, it would also be nice to have a way in which j...

59 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you