Upload files through traditional form submission:
- <form id= "uploadForm" action= "http://localhost:8080/cfJAX_RS/rest/file/upload" method= "post" enctype ="multipart/form-data">
- <h1 >Test pass Rest Interface upload file </h1>
- <p >Specify the file name: <input type ="text" name="filename" /></p>
- <p >Upload files: <input type ="file" name="file" /></p>
- <p >Keyword 1: <input type ="text" name="keyword" /></p>
- <p >Keyword 2: <input type ="text" name="keyword" /></p>
- <p >Keyword 3: <input type ="text" name="keyword" /></p>
- <input type ="submit" value="upload"/>
- </form>
However, the traditional form submission will lead to page refresh, but in some cases, we do not want the page refreshed, at this time we all use Ajax to request:
- $.ajax({
- url : "http://localhost:8080/STS/rest/user",
- type : "POST",
- data : $( '#postForm').serialize(),
- success : function(data) {
- $( '#serverResponse').html(data);
- },
- error : function(data) {
- $( '#serverResponse').html(data.status + " : " + data.statusText + " : " + data.responseText);
- }
- });
As above, form forms can be serialized by $(' postForm').serialize() to pass all the parameters in the form to the server.
However, in the above way, only general parameters can be passed, and the file stream of uploaded files can not be serialized and passed.
But now mainstream browsers are starting to support an object called FormData, with which we can easily 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. Using FormData objects, we can use JavaScript to simulate a series of form controls with some key-value pairs. We can also use XMLHttpRequest's send() Method to submit this "form" asynchronously. Compared with ordinary ajax, the biggest advantage of using FormData is that we can upload a binary file asynchronously.
Later versions of all major browsers already support this object, such as Chrome 7+, Firefox 4+, IE 10+, Opera 12+, Safari 5+.
|
Here's just one way to initialize FormData through the from form
<form enctype="multipart/form-data" method="post" name="fileinfo">
- var oData = new FormData(document.forms.namedItem("fileinfo" ));
- oData.append( "CustomField", "This is some extra data" );
- var oReq = new XMLHttpRequest();
- oReq.open( "POST", "stash.php" , true );
- oReq.onload = function(oEvent) {
- if (oReq.status == 200) {
- oOutput.innerHTML = "Uploaded!" ;
- } else {
- oOutput.innerHTML = "Error " + oReq.status + " occurred uploading your file.<br \/>";
- }
- };
- oReq.send(oData);
Using FormData, make Ajax requests and upload files
JQuery is used here, but older versions of JQuery, such as 1.2, are not supported. It is better to use 2.0 or newer versions:
- <form id= "uploadForm">
- <p >Specify the file name: <input type="text" name="filename" value= ""/></p >
- <p >Upload files: <input type="file" name="file"/></ p>
- <input type="button" value="upload" onclick="doUpload()" />
- </form>
- function doUpload() {
- var formData = new FormData($( "#uploadForm" )[0]);
- $.ajax({
- url: 'http://localhost:8080/cfJAX_RS/rest/file/upload' ,
- type: 'POST',
- data: formData,
- async: false,
- cache: false,
- contentType: false,
- processData: false,
- success: function (returndata) {
- alert(returndata);
- },
- error: function (returndata) {
- alert(returndata);
- }
- });
- }