Uploading files using Formdata objects via jquery Ajax

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/labnizejuly/p/5588444.html

FormDataobject, you can use a series of key-value pairs to simulate a complete form, and then use the XMLHttpRequest "form" to send it.

<formID= "Uploadform"enctype= "Multipart/form-data">    <inputID= "File"type= "File"name= "File"/>    <ButtonID= "Upload"type= "button">Upload</Button></form>

$.ajax ({    '/upload ',    ' POST ',    false,    New FormData ($ (' #uploadForm ') [0]),    false,    false}) . Done (function(res) {}). Fail (function

Here are a few things to note:

    • processDataSet to false . Because the data value is an FormData object, you do not need to process the data.
    • <form>tag to add a enctype="multipart/form-data" property.
    • cacheSet to false , the upload file does not need to be cached.
    • contentTypeSet to false . This is set to false because it is an <form> object constructed by a form, and the FormData property has already been declared enctype="multipart/form-data" .

After uploading, the server-side code needs to use the file get file input stream object from the query parameter name, because <input> it is declared in name="file" .

Uploading files using Formdata objects via jquery Ajax

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.