Re: Trigger Downloaddialog
From: Michael Borgwardt (brazil_at_brazils-animeland.de)
Date: 10/14/03
- Next message: Anders: "How to reach a document.images.<image> object from an Applet?"
- Previous message: tastle: "Re: Docking windows"
- In reply to: Frank Lorenz: "Trigger Downloaddialog"
- Next in thread: Frank Lorenz: "Re: Trigger Downloaddialog"
- Reply: Frank Lorenz: "Re: Trigger Downloaddialog"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 14 Oct 2003 14:38:56 +0200
Frank Lorenz wrote:
> Hi
>
> A servlet generated response is treaten differently each time. One time the
> webbrowser raises correctly a download dialog, wich enables the user to save
> a file. This happens when the data to download is small. The other time,
> instead to show a download dialog, the webbrowser shows the data directly as
> a new page. This happens with larger downloaddata.
>
> Therefore i suppose it depends on wrong caching adjustments i've made in the
> responseheader.
Close but not quite. You must set the header data *before* writing the the output
stream, because when the output buffer is filled the servlets begins to send the
reply and any headers set afterwards are ignored.
You have another bug waiting to happen:
> String csv_export = (String)aRequest.getSession().getAttribute(ATTR_KEY);
> size = csv_export.length();
>
> out = new ByteArrayOutputStream(size);
> out.write(csv_export.getBytes());
> out.writeTo(aResponse.getOutputStream());
> out.flush();
> out.close();
> aResponse.setContentLength(size);
If the platform default encoding ever happens to be one that uses more than one byte
for some characters (such as UTF-8) and such characters occur, your size value will be
wrong and the download may be truncated. Thus, you should specify the encoding explicitly
AND not rely on it using one byte per character (in case you ever want to change it).
Do it like this:
private static final String CHARSET = "ISO-8859-1";
...
out = new ByteArrayOutputStream(csv_export.length());
out.write(csv_export.getBytes(CHARSET));
out.flush();
// other headers here
aResponse.setContentLength(out.size());
aResponse.setContentType("text/comma-separated-values; charset=" + CHARSET);
out.writeTo(aResponse.getOutputStream());
out.close();
- Next message: Anders: "How to reach a document.images.<image> object from an Applet?"
- Previous message: tastle: "Re: Docking windows"
- In reply to: Frank Lorenz: "Trigger Downloaddialog"
- Next in thread: Frank Lorenz: "Re: Trigger Downloaddialog"
- Reply: Frank Lorenz: "Re: Trigger Downloaddialog"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|