Most web sites offer a smorgasbord of media
types to their users. Typical web content these days includes
Portable Document Format (PDF) files, word-processing documents,
audio files, movies, and Extensible Markup Language (XML). In some
cases, these alternative file types are stored in databases as
binary data, or streams of unencoded bytes. Web
developers cannot always provide their users with straightforward
hyperlinks to these files for downloading. The user chooses a link or
enters a URL in the browser's location field, and a
servlet or some other web component downloads the binary data to the
client. The client in most cases saves the data as a file, for use
later in document viewers and other applications.
The following recipes describe how to initiate this download method.
In a typical scenario, the servlet sets up a download whereby the
browser prompts the user with a "Save
As" dialog allowing him to save the files on his own
filesystem. These strategies, however, do not guarantee 100-percent
consistent behavior among web browsers. Some browsers allow the user
to precisely configure how he wants to handle handle certain file
types (such as a PDF document). For example, Opera 5 gives the user
all kinds of options for dealing with downloads, such as opening up
an external helper application, displaying a file using a plug-in, or
immediately downloading a file to a specified folder without first
opening up a "Save As" window.
Test how your web application works in various browsers, in response
to different browser configurations, as well as on different
platforms, so that you are aware of the different browser responses
elicited by the servlet.
The alternative to these strategies is simply providing a link to a
PDF file—for instance—if the data exists as a file. This
method, however, also places a great deal of the responsibility on
the client browser, and the user who configures the browser, for
managing the media type. While advanced users may like this approach,
it is rarely sufficient for less-experienced users.