You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Steve <st...@sjlt.co.uk> on 2009/05/13 13:25:27 UTC

Struts newbie - Advice on file downloading

Hi,

 

I'm a Struts 2 newbie and I need to write some code to download dynamically
created files in various formats (csv, txt and xml). I want the user to be
presented with a "Save As" dialog regardless of file type.

 

Does anyone have any advice / URL's for example code? I have found the
"Result Stream" documentation on the Struts site and various code snippets.
But I can't find any good complete examples.

 

Many Thanks,

 

Steve

Steve Higham

              

 


Re: Struts newbie - Advice on file downloading

Posted by Dale Newfield <da...@newfield.org>.
Richard Sayre wrote:
> <param name="contentDisposition">filename="Report.pdf"</param>

That is an invalid contentDisposition value.  We've recently fixed the 
documentation so that hopefully people will stop making this mistake.

If you want it displayed in the browser:
inline;filename="Report.pdf"
If you want the browser to save it:
attachment;filename="Report.pdf"

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts newbie - Advice on file downloading

Posted by Mike Altieri <mc...@yahoo.com>.
Hi Steve, 

I haven't implemented this in struts before; but have in the past built other java web-apps w/ the downloading of files...
the save-as dialog box is browser dependent (as Richard mentioned) and if a user has mucked w/ their mime-type settings you may not be able to force the regular save-as dialog...
(with the types you mentioned pdf, csv, etc this is very likely; also if you have to support different browsers this may work differently or even be set differently between them)

If you are in control of your end-user's environment (say a corporate env.) then this may not be a big deal; 
however if you aren't and you really need the save-as dialog in a rigid fashion; then perhaps putting in an applet is the way to go [this would even allow you to default to a certain directory regardless of the browser settings [in a req i have worked on in the past that was very important to my client since they're users weren't good at remember what directory they save things in] 

the user would have some extra work to accept the signed/self-signed applet on their first visit; but subsequent visits would be smooth and relatively consistent

Good luck!
-Mike



----- Original Message ----
> From: Richard Sayre <ri...@gmail.com>
> To: Struts Users Mailing List <us...@struts.apache.org>
> Sent: Wednesday, May 13, 2009 7:41:01 AM
> Subject: Re: Struts newbie - Advice on file downloading
> 
> Hi,
> 
> I have the following defined:
> 
> 
> method="generateReport">
>             
>                 application/pdf
>                 fileStream
>                 filename="Report.pdf"
>                 1024
> 
> 
> My action lookks like this
> 
> public String generateReport() {
> 
> //use api to build PDF
> //the api takes an output stream
> //so at the end of my method i have an output stream containg the file...
> 
> //bout is my output stream
> //file stream is a member of my Action (referenced in the  
> name="inputName">fileStream param)
> //fileStream is an InputStream
> fileStream = new ByteArrayInputStream(bout.toByteArray());
> 
> return SUCCESS;
> }
> 
> You will have to parameterize the XML to handle multiple file types:
> 
> ${mimeType}
> 
> Which you will set in your action.  Same goes for the other parameters.
> 
> I'm not sure how to get the browser to display a save dialog, I think
> it has to do with the mime type.  I think application/octet-stream
> will work.
> 
> -Rich
> 
> On Wed, May 13, 2009 at 8:55 AM, Steve wrote:
> > Hi,
> >
> >
> >
> > I'm a Struts 2 newbie and I need to write some code to download dynamically
> > created files in various formats (csv, txt and xml). I want the user to be
> > presented with a "Save As" dialog regardless of file type.
> >
> >
> >
> > Does anyone have any advice / URL's for example code? I have found the
> > "Result Stream" documentation on the Struts site and various code snippets.
> > But I can't find any good complete examples.
> >
> >
> >
> > Many Thanks,
> >
> >
> >
> > Steve
> >
> > Steve Higham
> >
> >
> >
> >
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts newbie - Advice on file downloading

Posted by Steve <st...@sjlt.co.uk>.
Hi,

Thanks to Richard, Mike and Dale for your help.

I've now got this working. The XML is downloading fine in my development
environment. I'll worry about other browsers and formats further downstream.

I've used contentType "text/xml" and contentDisposition
attachment;filename="myfile.xml" as suggested by Dale.

Cheers,

Steve

-----Original Message-----
From: Richard Sayre [mailto:richardsayre@gmail.com] 
Sent: 13 May 2009 12:41
To: Struts Users Mailing List
Subject: Re: Struts newbie - Advice on file downloading

Hi,

I have the following defined:

 <action name="generatePdf" class="com.abc.ReportAction"
method="generateReport">
            <result name="success" type="stream">
                <param name="contentType">application/pdf</param>
                <param name="inputName">fileStream</param>
                <param
name="contentDisposition">filename="Report.pdf"</param>
                <param name="bufferSize">1024</param>
</result>

My action lookks like this

public String generateReport() {

//use api to build PDF
//the api takes an output stream
//so at the end of my method i have an output stream containg the file...

//bout is my output stream
//file stream is a member of my Action (referenced in the  <param
name="inputName">fileStream</param> param)
//fileStream is an InputStream
 fileStream = new ByteArrayInputStream(bout.toByteArray());

return SUCCESS;
}

You will have to parameterize the XML to handle multiple file types:

 <param name="contentType">${mimeType}</param>

Which you will set in your action.  Same goes for the other parameters.

I'm not sure how to get the browser to display a save dialog, I think
it has to do with the mime type.  I think application/octet-stream
will work.

-Rich

On Wed, May 13, 2009 at 8:55 AM, Steve <st...@sjlt.co.uk> wrote:
> Hi,
>
>
>
> I'm a Struts 2 newbie and I need to write some code to download
dynamically
> created files in various formats (csv, txt and xml). I want the user to be
> presented with a "Save As" dialog regardless of file type.
>
>
>
> Does anyone have any advice / URL's for example code? I have found the
> "Result Stream" documentation on the Struts site and various code
snippets.
> But I can't find any good complete examples.
>
>
>
> Many Thanks,
>
>
>
> Steve
>
> Steve Higham
>
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts newbie - Advice on file downloading

Posted by Richard Sayre <ri...@gmail.com>.
Hi,

I have the following defined:

 <action name="generatePdf" class="com.abc.ReportAction"
method="generateReport">
            <result name="success" type="stream">
                <param name="contentType">application/pdf</param>
                <param name="inputName">fileStream</param>
                <param name="contentDisposition">filename="Report.pdf"</param>
                <param name="bufferSize">1024</param>
</result>

My action lookks like this

public String generateReport() {

//use api to build PDF
//the api takes an output stream
//so at the end of my method i have an output stream containg the file...

//bout is my output stream
//file stream is a member of my Action (referenced in the  <param
name="inputName">fileStream</param> param)
//fileStream is an InputStream
 fileStream = new ByteArrayInputStream(bout.toByteArray());

return SUCCESS;
}

You will have to parameterize the XML to handle multiple file types:

 <param name="contentType">${mimeType}</param>

Which you will set in your action.  Same goes for the other parameters.

I'm not sure how to get the browser to display a save dialog, I think
it has to do with the mime type.  I think application/octet-stream
will work.

-Rich

On Wed, May 13, 2009 at 8:55 AM, Steve <st...@sjlt.co.uk> wrote:
> Hi,
>
>
>
> I'm a Struts 2 newbie and I need to write some code to download dynamically
> created files in various formats (csv, txt and xml). I want the user to be
> presented with a "Save As" dialog regardless of file type.
>
>
>
> Does anyone have any advice / URL's for example code? I have found the
> "Result Stream" documentation on the Struts site and various code snippets.
> But I can't find any good complete examples.
>
>
>
> Many Thanks,
>
>
>
> Steve
>
> Steve Higham
>
>
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org