user3595632 user3595632 - 1 month ago 10
jQuery Question

jQuery: FormData() doesn't work

I made a really really simple example to test whether

jQuery
's
FormData
work or not.

html

<form id="frm" action="/fileupload" method="post">
<input type="file" name="uploadfile" />
<input type="file" name="uploadfile" />
<input type="button" id="uploadbutton" value="클릭" />
</form>


And I import
jQuery
cdn:

<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>


I select some image file from my computer.

And execute folloing code in
Chrome development tool
:

>> var form = $('#frm');
>> form

<form id="frm" action="/fileupload" method="post">
<input type="file" name="uploadfile" />
<input type="file" name="uploadfile" />
<input type="button" id="uploadbutton" value="클릭" />
</form>


>> var formData = new FormData(form);
>> formData


And it shows just,

FormData {}


It doesn't contain any values. It was empty . Why does it not work?

Answer

I reckon, you should try passing the element itself, but not the jQuery object:

var formData = new FormData(form[0]);