Formdata upload multiple images, learn to use Formdata

Source: Internet
Author: User

The Formdata object is facilitated for serializing tables and for creating the same data as the form format, which is, of course, for XHR transmissions.

Today we use Dropzone and formdata to implement multi-file upload capabilities.

var SAMP = null;        Dropzone object      SAMP = new Dropzone ("#dropzone",    {              URL: "#",  //Background response link              maxfiles:4,/    / The maximum number of files that can be transferred is  currently set to 1              maxfilesize:2048,//File size limit              acceptedfiles: ". Png,.jpg,.gif,.jpeg,.bmp",// File format Restrictions              Autoprocessqueue:false,  //files are automatically passed back to background              myawesomedropzone:false,    })

We set the file not to upload automatically, but through the control button to implement the upload function. First we create a Dropzone object, set the maximum number of uploaded files, file size, and so on.

  var myformdata = new FormData ()

Create a form data object

Samp.on ("Addedfile", function (file) {    myformdata.append (file.name, file)})

Registers the Addedfile event with the Dropzone object, and when the file is uploaded, the append () function of the Formdata object is the addition of the image object to the Myformdata pairs in the form of a key-value pair.

$.ajax ({
Type: ' POST ', url: "/upload-img/", Data:myformdata, processdata:false,//* must write contenttype:false,//* must write
Success:function (data) {
...
},
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
...
}
});

When using the Formdata object to pass data through Ajax to the background, you must set the "Processdata:false,contenttype:false," two items in the option, or you will get an error.

  processDataSet to false . Because the data value is an FormData object, you do not need to process the data.

contentType set 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"   .

Formdata upload multiple images, learn to use Formdata

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.