In earlier examples, one had to call response.setContentLength() with
the number of bytes being passed.  I have the following questions
regarding this:

1. What are the consequences if I get the length wrong?  Does it have to
be exact?
2. Can the setContentLength() be called after I have done all the
writes, or must it be done before the writes?  If I can call it
beforehand, then I can compute the length as I go.  Otherwise, it will
be more difficult.

Thanks,

- Brendan

-----Original Message-----
From: CONNER, BRENDAN (SBCSI) 
Sent: Tuesday, October 25, 2005 12:15 PM
To: 'MyFaces Discussion'
Subject: RE: AW: FileDownload capability?


And it's even easier if one is generating a text file (rather than a
binary file), for example, when writing out a comma-separated file for
use in a spreadsheet, since one can use
response.getWriter().print(String s) and
response.getWriter().println(String s).

- Brendan

-----Original Message-----
From: Nico Krijnen [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, October 25, 2005 3:00 AM
To: 'MyFaces Discussion'
Cc: [EMAIL PROTECTED]
Subject: RE: AW: FileDownload capability?


It is actualy much easier in JSF. You just need to have an action method
in
a (managed) bean, like this:


public String downloadFile() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (!facesContext.getResponseComplete()) {
                String fileName = "myfile.pdf";

                ServletContext servletContext = (ServletContext)
facesContext.getExternalContext().getContext();
                String contentType =
servletContext.getMimeType(fileName);

                HttpServletResponse response = (HttpServletResponse)
facesContext.getExternalContext().getResponse();
                response.setContentType(contentType);
                response.setHeader("Content-Disposition",
"attachment;filename=\"" + fileName + "\"");

                try {
                        InputStream in = /* get your data */;
                        ServletOutputStream out =
response.getOutputStream();

                        byte[] buf = new byte[512];
                        int bytesRead;
                        while ((bytesRead = in.read(buf, 0, bufSize)) !=
-1)
{
                                out.write(buf, 0, bytesRead);
                        }

                        out.flush();
                        facesContext.responseComplete();
                } catch (IOException e) {
                        throw new RuntimeException(e);
                }
        }
        return null;
}


Then you can simply call the method from a commandLink or commandButton


<h:commandLink action="#{yourBean.downloadFile}">
        <h:outputText value="download" />
</h:commandLink>


Nico


-----Oorspronkelijk bericht-----
Van: news [mailto:[EMAIL PROTECTED] Namens Werner Punz
Verzonden: maandag 24 oktober 2005 23:34
Aan: users@myfaces.apache.org
Onderwerp: Re: AW: FileDownload capability?

It is rather easy, I usually have a download servlet for this
which generates the appropriate html code, I do not have the code handy
currently
but you basically set the header mimetype to either your filetype or
application/octed
stream, then pass down the content length (this is important because
otherwise our all beloved IE
has some problems on certain filetypes)
and then you basically pass the stream as content down as embedded
binary
data.

You also can achieve that with a phaselistener if you feel uneasy to do
it
over
a separate servlet.

All you then have to do is to link to the servlet or phase listener with
a
linke
and a target="_new"

Werner


[EMAIL PROTECTED] wrote:
> I have nearly the same problem:
> 
> I have the link of a sample file (pdf, doc or something else) in the
database.
> Now the user should have the possibility to open this file via a
CommandButton or CommandLink
> 
> I'm not quite sure, how I can do this?
> Thx for help
> 



Reply via email to