title: Prompt browser to download a file throgh Jquery
body: <p>When I click on a link , I want to call my API in my "JobDownload" servlet and download the response as a file in browser. The API returns a multipart response which I convert to Input Stream. I am able to show contents returned after calling servlet in console.log() below, but unable to open browser download box.</p>
  
  <p>I call like this :</p>
  
  <pre><code>$("#filename").click(function(){
      alert("Download initiated");
      $.get('jobDownload', {'key': jobDownId,'name':jobFileName}, 
          function (data) {
             console.log("Data Downloaded"+data);                                
           });
  });
  </code></pre>
  
  <p>My servlet looks like this :</p>
  
  <pre><code> protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
          String jobId = request.getParameter("key");
          String fileName = request.getParameter("name");
  
          String headerKey = "Content-Disposition";
          String headerValue = String.format("attachment; filename=\"%s\"", fileName);
          response.setHeader(headerKey, headerValue);
          response.setContentType("text/plain");
  
          System.out.println("\n\nJob ID is :"+jobId+"\nFilename is :"+fileName);
  
          // Calling my API which will return an InputStream
          InputStream jobResult = getJobResultById(jobId);
  
          OutputStream os = response.getOutputStream();
          System.out.println("\n\nInputStream of job is here");
  
          // Stream the inputStream to outputStream
          streamOutput(jobResult, os);
          System.out.println("\n\nOutStream of job is here");
      }
  </code></pre>
  
  <p>And my streamOutput method is like this :</p>
  
  <pre><code>public void streamOutput(InputStream is, OutputStream os) {
          BufferedInputStream dis = null;
          try {
              dis = new BufferedInputStream(new DataInputStream(is));
              byte[] buf = new byte[8192];
              int nread = dis.read(buf);
              while (nread != -1) {
                  os.write(buf, 0, nread);
                  nread = dis.read(buf);
              }
              // flush the output as soon as possible to prevent client timeouts
              // we can delay cleaning up the inputstream 
              os.flush();
          } catch (IOException ioe) {
              ioe.printStackTrace();
          }
          dis = null;
      }
  </code></pre>
  
  <p>I am not getting that browser download dialog box which user should click to download this result. Do I need to add anything in my Jquery method or add something in my java code </p>