2 Replies Latest reply on Aug 13, 2002 2:55 AM by Joel Vogt

    How to get a popup download dialog box from a servlet

    Ron Jawanda Novice

      I have a servlet that creates a file within the web context.
      I want to present the user with a download popup dialog box that allows then to "save as" or view.
      The file is 15 MB so I don't want to just stream it to them.
      I do get the binary data on the web browser but it is the actual binary content of the PDF file. This isn't so good. I want to view the actual PDF representation.
      ie: it should open Adobe PDF viewer or ask the user to save as.

      Anyone know how.

      I tried setting the content type to application/pdf
      and application/octet-stream

      Then tried pushing to the output stream but no dialog box.'

      I really hope you can help me.

        • 1. Re: How to get a popup download dialog box from a servlet
          Russell Simpkins Newbie

          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")) {
          // construct the FileAsset.
          FileAsset theFile = new FileAsset(new Long(req.getParameter("fpAssetID")));
          if (theFile.isException()) {
          // set our headers.
          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)) {

          theFile = null;

          • 2. Re: How to get a popup download dialog box from a servlet
            Joel Vogt Master

            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.