title: Download Excel File Via Servlet
body: <p>I am using the below code invoked via ajax call for create my excel file.On click the download button excel file gets generate at the root location. But I am not able to see the prompt for user to save/save as the file. I can see the response tab of browser where the content of excel are coming. But I want that Save As dialog option to come. Any correction in below code that can solve this issue?</p>
  
  <pre><code>            final Date date = new Date();
              final String generateDate= new SimpleDateFormat("yyyy-MM-dd").format(date);
              final String filename = form_name+"-extraction-"+generateDate.toString()+".xls";
              final FileOutputStream fileOutputStream = new FileOutputStream(filename);
              workbook.write(fileOutputStream);
              downloadFile(filename, response);
  </code></pre>
  
  <p>Below is download file method :</p>
  
  <pre><code>private void downloadFile(final String fileName, final SlingHttpServletResponse response){
          try {
              final File f = new File(fileName);
              response.setContentType("application/vnd.ms-excel");
              response.setHeader("Content-Disposition", "inline; filename="+fileName);
              response.setHeader("Pragma", "public");
              response.setHeader("Cache-Control", "no-store");
              response.addHeader("Cache-Control", "max-age=0");
              FileInputStream fin = null;
              try {
                  fin = new FileInputStream(f);
              } catch (final FileNotFoundException e) {
                  e.printStackTrace();
              }
              final int size = 1024;
              try {
                  response.setContentLength(fin.available());
                  final byte[] buffer = new byte[size];
                  ServletOutputStream os = null;
  
                  os = response.getOutputStream();
                  int length = 0;
                  while ((length = fin.read(buffer)) != -1) {
                      os.write(buffer, 0, length);
                  }
                  fin.close();
                  os.flush();
                  os.close();
              } catch (final IOException e) {
                  e.printStackTrace();
              }
          }catch (final Exception ex){
              LOGGER.error("ERROR IS ::: {}",ex.getMessage());
      }
  </code></pre>