Question : servlet download file with javascript enabled

Hi,

This is what I need to achieve.

I have a client side code that calls a servlet with parameters. The servlet gets the parameters and process it. Generate a zip file and then send it back to client for download. A "Save as" window will appear. It runs great with small download file because the "Save As" window will come out right away. But with larger download file, I would like to have a waiting window so the user will know something is being downloaded. For this purpose, I need to pass some javascript back from the servlet to tell the client that the zip file is done generated so the waiting window can be closed.

This is the piece of code I'm using. I'm able to download the zip file but I don't know how I can include javascript in the response? I've tried multiple server push but according to some research, IE doesn't work for multiple server push. By the way, my front end is using smartgwt. Thanks a lot!

    response.setContentType("text/html");
   
    ServletOutputStream out = null;
    try {
    out = response.getOutputStream();
//    out.write(("<html>").getBytes());
//    out.write(("<head>").getBytes());
//
//    out.write(("<script type=\"text/javascript\">").getBytes());
//    out.write(("if (parent.uploadComplete) parent.exportComplete('"+ "filename.txt" + "');").getBytes());
//    out.write(("</script>").getBytes());
//
//    out.write(("</head>").getBytes());
   
    } catch (Exception e) {
      e.printStackTrace();
    }

    List<String> exportList = new ArrayList<String>();
    String selectedRec = request.getParameter(request.getParameter("name"));
    String fileId = "";
   
    try {
      JSONTokener tok = new JSONTokener(selectedRec);
      JSONArray jarray = new JSONArray(tok);
      for (int i = 0; i < jarray.length(); i++) {
        String path = jarray.getString(i);
        logger.debug("jarray " + i + " is " + path);
        int type = path.split("/").length;
        fileId = path.split("/")[2];
        if (type == 3) {
          exportList.add("file=" + path);
        } else if (type == 5) {
          exportList.add("document=" + path);
        } else if (type == 7) {
          exportList.add("page=" + path);
        }
      }
    } catch (JSONException e) {
      logger.error("Export error", e);
      e.printStackTrace();
    }

     try {
      HashMap<String, byte[]> outMap =
          ExportUtils.getContentFromRepo("export", exportList);

      // Write to the zip file
      ByteArrayOutputStream baos = ExportUtils.zipFiles(outMap);

      response.setHeader("Content-Disposition", "attachment; filename="
          + fileId + ".zip");
      try {
        //OutputStream out = response.getOutputStream();
        out.write(baos.toByteArray());
        //out.write(("</html>").getBytes());
        out.flush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (Exception e) {
      logger.error("Export error", e);
      e.printStackTrace();
    }

Answer : servlet download file with javascript enabled

You may return first the path/url of the file with some messages (size, filename,...) to the browser then the browser receiving a valid message and url start downloading the file with the url provided
Random Solutions  
 
programming4us programming4us