FormData uploads files,

Source: Internet
Author: User

FormData uploads files,

Form submission:

<Form id = "uploadForm" action = "http: // localhost: 8080/cfJAX_RS/rest/file/upload "method =" post "enctype =" multipart/form-data "> However, page Jump is required.


Ajax:

$.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 shown above, the form can be serialized using $ ('# postform'). serialize () to pass all parameters in the form to the server. However, the above method can only pass common parameters. The file stream uploaded to the file cannot be serialized and transmitted. However, mainstream browsers now support an object named FormData. With this FormData, we can easily upload files using Ajax.

FormData. JQuery is used here.

Html code

<Form id = "uploadForm"> <p> specify the file name: <input type = "text" name = "filename" value = ""/> </p> <p> upload a file: <input type = "file" name = "file"/> </p> <input type = "button" value = "Upload" onclick = "doUpload () "/> </form>
Js Code
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);            }       });  }  


To be continued...

Top
0
Step on
0
View comments

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.