Upload files via Ajax and make Ajax requests using FormData

Upload files via AjaxUse FormData for Ajax requests 
Upload files via traditional form submission:
  1. <form id"uploadForm" action"http://localhost:8080/cfJAX_RS/rest/file/upload" method"post" enctype ="multipart/form-data">  
  2.      <h1 >Test Passed Rest Interface Upload File </h1>  
  3.      <p >Specify the file name: <input type ="text" name="filename" /></p>  
  4.      <p >Upload file: <input type ="file" name="file" /></p>  
  5.      <p >Keyword 1: <input type ="text" name="keyword" /></p>  
  6.      <p >Keyword 2: <input type ="text" name="keyword" /></p>  
  7.      <p >Keyword 3: <input type ="text" name="keyword" /></p>  
  8.      <input type ="submit" value="upload"/>  
  9. </form>  

 

However, traditional form submissions can cause pages to refresh, but in some cases, we don't want pages to refresh, in which case we all request them using Ajax:
  1. $.ajax({  
  2.      url : "http://localhost:8080/STS/rest/user",  
  3.      type : "POST",  
  4.      data : $( '#postForm').serialize(),  
  5.      success : function(data) {  
  6.           $( '#serverResponse').html(data);  
  7.      },  
  8.      error : function(data) {  
  9.           $( '#serverResponse').html(data.status + " : " + data.statusText + " : " + data.responseText);  
  10.      }  
  11. });  

 

As mentioned above, the form form form can be serialized with $('#postForm').serialize() to pass all parameters from the form to the server.
 
However, in this way, only general parameters can be passed, and the file stream of the uploaded file cannot be serialized and passed.
But now mainstream browsers are starting to support an object called FormData, which makes it easy to upload files using Ajax.

 

 

About FormData and its usage

What is FormData?Let's take a look at the introduction on Mozilla.
XMLHttpRequest Level 2 adds a new interface, FormData. With the FormData object, we can use JavaScript to simulate a series of form controls using key-value pairs. We can also use XMLHttpRequest's send() Method submits this "form" asynchronously. The best advantage of using FormData over normal ajax is that we can upload a binary asynchronously.
 
This object is already supported in newer versions of all major browsers, such as Chrome 7+, Firefox 4+, IE 10+, Opera 12+, Safari 5+.
 
See: https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/FormData

 

 

Here's just one way to initialize FormData through the from form
<form enctype="multipart/form-data" method="post" name="fileinfo">
  1. var oData = new FormData(document.forms.namedItem("fileinfo" ));  
  2. oData.append( "CustomField""This is some extra data" );  
  3. var oReq = new XMLHttpRequest();  
  4. oReq.open( "POST""stash.php" , true );  
  5. oReq.onload = function(oEvent) {  
  6.       if (oReq.status == 200) {  
  7.           oOutput.innerHTML = "Uploaded!" ;  
  8.      } else {  
  9.           oOutput.innerHTML = "Error " + oReq.status + " occurred uploading your file.<br \/>";  
  10.      }  
  11. };  
  12. oReq.send(oData);  

 

See: https://developer.mozilla.org/zh-CN/docs/Web/Guide/Using_FormData_Objects
 
 
Using FormData, make Ajax requests and upload files
JQuery is used here, but older versions of JQuery, such as 1.2, are not supported. Better use version 2.0 or later:
  1. <form id"uploadForm">  
  2.       <p >Specify the file name: <input type="text" name="filename" value""/></p >  
  3.       <p >Upload file: <input type="file" name="file"/></ p>  
  4.       <input type="button" value="upload" onclick="doUpload()" />  
  5. </form>  

 

  1. function doUpload() {  
  2.      var formData = new FormData($( "#uploadForm" )[0]);  
  3.      $.ajax({  
  4.           url: 'http://localhost:8080/cfJAX_RS/rest/file/upload' ,  
  5.           type: 'POST',  
  6.           data: formData,  
  7.           async: false,  
  8.           cache: false,  
  9.           contentType: false,  
  10.           processData: false,  
  11.           success: function (returndata) {  
  12.               alert(returndata);  
  13.           },  
  14.           error: function (returndata) {  
  15.               alert(returndata);  
  16.           }  
  17.      });  
  18. }  

Keywords: REST JQuery Javascript Firefox

Added by heropage on Wed, 05 Jun 2019 20:08:05 +0300