-----Original Message-----
The content-length is passed in the response header. Therefore it cannot
be
set once you have written data to the response body (after all, the
header
is written before the body, for details see http spec). 

If you know the length in advance you should set it (for example when
streaming data from a file or from a BLOB field in a database). If you
are
generating data on the fly and cannot predict the length, simply do not
call
setContentLength. Your browser will still receive the file, it just
won't
show a download percentage as it does not know how much it will receive.
Try
it out with a few MB of data, you'll see what I mean.
-----/Original Message-----
The problem with the missing content-length is that certain browsers are
tempted to redo the request, just because the length is missing...
At least some reports on problems with double-requests from IE to PDF-
producing servlets imply this...

regards
Alexander


-----Original Message-----
-----Oorspronkelijk bericht-----
Van: CONNER, BRENDAN (SBCSI) [mailto:[EMAIL PROTECTED] 
Verzonden: dinsdag 25 oktober 2005 21:46
Aan: MyFaces Discussion
Onderwerp: RE: AW: FileDownload capability?

I think I have most of the answers to my own questions, after consulting
the O'Reilly book "Java Servlet Programming":

1. The response, if set, has to be exact.  However, it does not have to
be set.  Using it allows Servlets to be able to take advantage of
persistent connections, if available.
2. It must be set before sending the response body.

So, given this, does (2) above imply that, if done, setting the length
has to precede the first write?  Or it just has to be done before
calling responseComplete()?

- Brendan

P.S. I realize this is more of a Servlet question than a MyFaces
question.  Sorry about that.

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


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: [email protected]
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