Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to submit Form with AJAX Using enctype="multipart/form-data"?

how to submit Form with AJAX Using enctype="multipart/form-data"?

like image 310
Sureshkumar Menon Avatar asked Apr 14 '11 12:04

Sureshkumar Menon


1 Answers

Short answer: you don't. You cannot upload files via AJAX.

The usual workaround is to set the target of your form to a hidden iframe and submit the form there, using a normal, non-AJAXy POST, to achieve the desired effect:

<form target="hiddenIframe" method="post" enctype="multipart/form-data">
    ...
</form>
<iframe name="hiddenIframe" id="hiddenIframe" style="display: none;" />

There's a jQuery plugin that uses this technique.

Edited to add:

XMLHttpRequest level 2 added support for uploading files via AJAX, and its browser support is now good and growing. Here's a browser support overview.

like image 143
Henning Avatar answered Oct 16 '22 04:10

Henning