title: Javascript: How to upload file onsubmit()?
body: <p>I have a JSP page with <code>&lt;form&gt;</code> that uploads a file on submit.
  But the <strong>upload</strong> function is not uploading any file to the server at the path <em>/uploads</em>.</p>
  <p><strong>HTML form:</strong></p>
  <pre><code>&lt;form action=&quot;./index.jsp&quot; onsubmit=&quot;uploadFile()&quot;&gt;
          &lt;input type=&quot;file&quot; class=&quot;form-control-file &quot; placeholder=&quot;Choose file&quot; id=&quot;excelInput&quot;&gt;
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Run Profiling&lt;/button&gt;
   &lt;/form&gt;
  </code></pre>
  <p><strong>Javascript function to upload file:</strong></p>
  <pre><code>&lt;script&gt;
    function uploadFile(){
        let excel = document.getElementById(&quot;excelInput&quot;).files[0];  // file from input
        alert(excel);
        let req = new XMLHttpRequest();
        let formData = new FormData();
          try{
                formData.append(&quot;excel&quot;, excel);                                
                req.open(&quot;POST&quot;, 'D:\\eclipse\\workspace\\Project2\\WebContent\\uploads');
                req.send(formData);
          }
          catch(e){ 
              alert(e);
          }
    }
    &lt;/script&gt;
  </code></pre>
  <p>It is not going to the <code>catch()</code> section. But it isn't uploading either.<br />
  Is there anything missing ?</p>