js create formdata code example
Example 1: formdata js
var form = $('form')[0];
var formData = new FormData(form);
or specify exact data for FormData();
var formData = new FormData();
formData.append('section', 'general');
formData.append('action', 'previewImg');
formData.append('image', $('input[type=file]')[0].files[0]);
Sending form
Ajax request with jquery will looks like this:
$.ajax({
url: 'Your url here',
data: formData,
type: 'POST',
contentType: false,
processData: false,
});
After this it will send ajax request like you submit regular form
with enctype="multipart/form-data"
Update: This request cannot work without type:"POST" in options since all
files must be sent via POST request.
Example 2: add formdata javascript
var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456);
formData.append("userfile", fileInputElement.files[0]);
var content = '<a id="a"><b id="b">hey!</b></a>';
var blob = new Blob([content], { type: "text/xml"});
formData.append("webmasterfile", blob);
var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);