debute debute - 2 months ago 6
HTML Question

Get content of FormData with data set from form element

I have

formData
from
<form>
element and I want to get content of elements inside form, but with
formData
. As you can see, I'm not using
formData.append()
. Also, please, do not post answers with
form.find('input').val()
, because it doesn't answer my problem. I will use that
formData
in the ajax, that's why I am here.

Simple example:


$(function() {
var form = $('#test');

form.on('submit', function(e) {
e.preventDefault();
var formData = new FormData(form[0]);
console.log(formData.get('file'));
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form enctype="multipart/form-data" id="test">
<input type="file" name="file" accept="image/*">
<input type="submit">
</form>




Answer
$.each($("input[type='file']")[0].files, function(i, file) {
    data.append('file', file);
});
Comments