title: How to send file using javascript
body: <p>I am trying to make an upload page which takes file to upload. I am using Spring framework here my query is on Upload button I am calling a JavaScript method which should send my file to controller using jQuery AJAX. Is there any way to pass this through JavaScript?</p>
  
  <p>Following is the code which I am trying.</p>
  
  <pre><code>&lt;body&gt;
      &lt;div style="text-align: center; margin-top: 60px;"&gt;
          &lt;form enctype="multipart/form-data"&gt;
              Select file:
              &lt;input type="file" name="dataFile" id="fileAttachment"/&gt;&lt;br/&gt;&lt;br/&gt;
                  &lt;div style="text-align: center; margin-top: 100px;"&gt;
                      &lt;input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" /&gt;
                  &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>&lt;script language="Javascript"&gt;
  function uploadAttachment(){
      var Name = jQuery('#Name option:selected').text();
      jQuery.post('upload',{Name:Name}
      function(data){
      if(data==1)
      alert("success");
      else
      alert("failed");
      });
  }
      &lt;/script&gt;
  </code></pre>
  
  <blockquote>
    <p>on controller.java page following is the code written</p>
  </blockquote>
  
  <pre><code>@RequestMapping(value = "upload", method=RequestMethod.POST)
          public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
  @RequestParam("Name") String Name){
  System.out.println(Name);
  }
  </code></pre>