title: Downloading csv file from server
body: <p>I have a system in which frontend is in javascript/ajax and the backend is written using REST(jersey).</p>
  
  <p>I want to download a file using my system. I have searched various forums and implemented the REST web method as follows :</p>
  
  <pre><code>@POST
  @Produces({"text/csv"})
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Path("getcsv")
  public Response  getcsv(
          @FormParam("usernamecsv") String userid,
          @FormParam("filename") String filename
          )
  {
      final File fobj = new File("c:/" +userid + "/output/" + filename);
      try
      {
          final FileInputStream f =  new FileInputStream(fobj);
  ContentDisposition cd =  
          ContentDisposition.type("file").fileName(fobj.toString()).build(); 
  Response response = Response
  .ok()
  .lastModified(new Date(fobj.lastModified()))
  .type("application/octet-stream")
  .header("Content-Disposition", cd)
  .entity(f)
  .build();
  return response;
      }
      catch (FileNotFoundException e1)
      {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
      return null;
  </code></pre>
  
  <p>}</p>
  
  <p>Initially I had used the StreamingOutput class and implemented the write method for it. In that method I had returned the string read from the file. But I have found no difference between that and the above implementation. Both return the string inside the file.</p>
  
  <p>In my frontend this is what I have done</p>
  
  <pre><code>&lt;!DOCTYPE html &gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;script type="text/javascript" src="jquery-1.7.1.min.js"&gt;&lt;/script&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
  function fun1()
  {
  $.ajax({
      url: '/RestWSGS/jersey/UserAuthentication/getcsv',
      async: false,
      data: $('#form2').serialize(),
      type: 'POST',
      cache: false,
      contentType: "application/x-www-form-urlencoded",
      processData: false,
      dataType: "text",
      success: function(data)
       {
          var iframe;
          iframe = document.getElementById("hiddenDownloader");
          if (iframe === null)
          {
              var iframe;
              iframe = document.getElementById("hiddenDownloader");
              if (iframe === null)
              {
                  iframe = document.createElement('iframe');  
                  iframe.id = "hiddenDownloader";
                  //iframe.style.visibility = 'hidden';
                  $("#mydiv").append(iframe);
              }
              iframe.src = "http:\\localhost:8080\\c:\abc@abc.com#26 8 2012 13 5 49/gr1/output/test.csv";  
  //iframe.src = data; 
  
          }
  
          alert('Hi');
  
       }
   });
  }
  $(function()
          {
              $(document).delegate("#mydiv","click",function(ev)
              {
              fun1();
  
              });
          });
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="mydiv" style='position:absolute;width:20px;height:20px;background:black'&gt;&lt;/div&gt;
  &lt;form id="form2" enctype="multipart/form-data" method="post" &gt;
   &lt;input id ="usernamecsv" name="usernamecsv" type="hidden"  value="abc@abc.com#26 8 2012 13 5 49/gr1"/&gt;
    &lt;input id ="filename" name="filename" type="hidden"  value="test.csv" /&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My problem is that I am getting a file not found response( though the data variable has the content of the file), if I assign the iframe.src with the data variable</p>
  
  <p>If I give the uri of the webservice (which I have given to the ajax call) to the iframe.src, I dont know how I will send the parameters.</p>
  
  <p>I really need to show a download prompt to the user and allow him to save the file to the local filesystem. I dont think my REST is right either as it should show a file object in firebug during POST not the contents of the file!</p>
  
  <p>I can just return the string and populate any textarea/ div with it and then ask the user to copy paste it to his file! but that does not look smooth or elegant!</p>
  
  <p>Please help,
  Kavita</p>
  
  <p>EDIT:
  Tried changing @Produces and type in Response.type() to "application/csv" as well as "application/something" but it always tries to append the contents of the file returned by the server to the current url and opens a search as file is obviously not found!!!</p>
  
  <p>EDIT:
  I tried converting POST to GET in the REST and used the data returned. Then it returns a DOCUMENT but still cannot open the file</p>