You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "CONNER, BRENDAN (SBCSI)" <bc...@sbc.com> on 2005/10/21 21:29:04 UTC

FileDownload capability?

We have an application requirement saying that the user needs the
ability to push a button on our report page to download the report data
into a local file on the user's machine suitable for loading into a
spreadsheet.  How can something like this be done?

- Brendan

Re: FileDownload capability?

Posted by Mike Kienenberger <mk...@gmail.com>.
Also, Sylvaine was talking about encapsulating this into a download
component, but it hasn't happened yet.   Feel free to beat him to it
:)

-Mike

On 10/21/05, Mike Kienenberger <mk...@gmail.com> wrote:
> Here's one way.
>
> Note that facesContext.getResponseStream() is only valid during the
> render-response phase, so you'll have to access the HttpRequest
> directly (not sure how it works for portlets).
>
>
>     public String downloadContentData()
>     {
>         Content selectedContent =
> (Content)this.announcementContentDataList.getRowData();
>         FacesContext facesContext = FacesContext.getCurrentInstance();
>         // OutputStream responseStream = facesContext.getResponseStream();
>
>         try
>         {
>             OutputStream responseStream =
> ((HttpServletResponse)facesContext.getExternalContext().getResponse()).getOutputStream();
>             if (null == responseStream)  throw new
> AbortProcessingException("responseStream is null");
>
>             HttpServletResponse response =
> (HttpServletResponse)facesContext.getExternalContext().getResponse();
>             response.setContentType(selectedContent.getContentType());
>             response.setHeader("Content-Disposition","attachment;filename=\""
> + selectedContent.getContentId() + "\"");
>             response.setContentLength(selectedContent.getContentData().getData().length);
>             responseStream.write(selectedContent.getContentData().getData());
>
>             response.flushBuffer();
>         }
>         catch (IOException exception)
>         {
>             // TODO Auto-generated catch block
>             exception.printStackTrace();
>         }
>
>         facesContext.responseComplete();
>
>         return null;
>     }
>
>
>
> On 10/21/05, CONNER, BRENDAN (SBCSI) <bc...@sbc.com> wrote:
> > We have an application requirement saying that the user needs the
> > ability to push a button on our report page to download the report data
> > into a local file on the user's machine suitable for loading into a
> > spreadsheet.  How can something like this be done?
> >
> > - Brendan
> >
>

Re: FileDownload capability?

Posted by Mike Kienenberger <mk...@gmail.com>.
Here's one way.

Note that facesContext.getResponseStream() is only valid during the
render-response phase, so you'll have to access the HttpRequest
directly (not sure how it works for portlets).


    public String downloadContentData()
    {
        Content selectedContent =
(Content)this.announcementContentDataList.getRowData();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        // OutputStream responseStream = facesContext.getResponseStream();

        try
        {
            OutputStream responseStream =
((HttpServletResponse)facesContext.getExternalContext().getResponse()).getOutputStream();
            if (null == responseStream)  throw new
AbortProcessingException("responseStream is null");

            HttpServletResponse response =
(HttpServletResponse)facesContext.getExternalContext().getResponse();
            response.setContentType(selectedContent.getContentType());
            response.setHeader("Content-Disposition","attachment;filename=\""
+ selectedContent.getContentId() + "\"");
            response.setContentLength(selectedContent.getContentData().getData().length);
            responseStream.write(selectedContent.getContentData().getData());

            response.flushBuffer();
        }
        catch (IOException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }

        facesContext.responseComplete();

        return null;
    }



On 10/21/05, CONNER, BRENDAN (SBCSI) <bc...@sbc.com> wrote:
> We have an application requirement saying that the user needs the
> ability to push a button on our report page to download the report data
> into a local file on the user's machine suitable for loading into a
> spreadsheet.  How can something like this be done?
>
> - Brendan
>