Ilan Azbel wrote:
Hello,
I would like a vm to return a binary file and thereby instruct the browser
not to display it, but rather to download it. That is, ask the user if they
would like to "Open" or "Save" the file.
I would like to do this without any redirecting statements - so as soon as a
user requests a certain .vm file, the corresponding java creates some type
of binary data, and the browser immediately asks whether to open or save
this data.
Thanks
Ilan
Hi,
first, be advised to configure your servlet container to serve binary
data itself or to use your own servlet to create the binary content.
Solutions using Velocity tend to be a bit fragile (character encoding,
whitespace handling, first and only to write to the output stream,
changing response headers, etc.).
I once tried the following, but at some point in velocity history the
response stream was changed - to my memory it was from Writer to
OutputStream.
(please notice the several context tools used to do the task).
#set( $l_file = $ServletContext.getRealPath("/images/$l_fileName") )
#if( $File.fileExists($l_file) )
#set( $l_fileData = $File.fileRead($l_file) )
### check for other image types
$res.setHeader("Content-type", "image/jpeg")
$data.Out.write($l_fileData)
#else
#error( "File $l_file does not exist and cannot be displayed" )
#end
the I rewrote it to:
<br>
<img src="#image($l_fileName)" border="0" />
using a globalMacros.vm:
#macro( image $fileName )/$WEBAPP/images/$fileName#end
This lets the HTTP server do its job.
You could use the first approach in velocity, by placing the outputStream
object into the context and letting the template add the (binary) data.
But be aware! this construct is really fragile - mainly due to the
inconsitent whitespace handling of velocity. Also problesm arise when
using the VelocitylayoutServlet, wher you have to instruct to skip the
layout in such a case...
If you wish an example of creating binary content with a servlet
check the literature, servlets.com, or I can send you a servlet code
that creates button+text images on the fly.
--
:) Christoph Reck
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]