-
1. Re: How to get a popup download dialog box from a servlet
russellsimpkins Aug 12, 2002 10:57 PM (in response to rgjawanda)I may be mistaken, but I believe that you will have to change the extension to force a dialog box. IE and others default behavior is to open associated content in the browser as is the case with PDF. PDF files are registered on your desktop with the acrobat viewer plugin, so they open up. If you choose an extension the browser does not know about, it will prompt you to save the file.
If you are only seeing binary content, you've got an issue with your code. I had this happen to me when I first tried to stream binary data with a JSP, which I later learned you can not do. It comes down to the jsp's tacking on an extra \r\n no matter what you do. The extra \r\n invalidated my pdf as binary data and caused the browser to render it as text.
Here is the jist of my working servlet which serves up any binary data. I have files stored as BLOBs in a db. The file object has a writeDate method which takes an outputstream. Hope this helps you.
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
// no work to do if there is no asset_id.
if (req.getParameter("fpAssetID") == null || req.getParameter("fpAssetID").equals("0")) {
return;
}
// construct the FileAsset.
FileAsset theFile = new FileAsset(new Long(req.getParameter("fpAssetID")));
if (theFile.isException()) {
return;
}
// set our headers.
res.setContentLength(theFile.getLength());
res.setContentType(theFile.getContentType() + "; name=\"" + theFile.getFileName() + "\";");
res.addHeader("Content-Disposition", "filename=\"" + theFile.getFileName() + "\";");
// get an OutputStream
ServletOutputStream out = res.getOutputStream();
BufferedOutputStream buff = new BufferedOutputStream(out);
// write the file to the OutputStream.
if (!theFile.writeData((OutputStream)out)) {
Data.debug(theFile.getExceptionString());
}
out.flush();
out.close();
theFile = null;
return;
} -
2. Re: How to get a popup download dialog box from a servlet
joelvogt Aug 13, 2002 2:55 AM (in response to rgjawanda)One thing to be careful of here is how IE treats servlet calls. It might actually do from between 1 to 3 calls to retrieve that same data.