Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - receiving the $_FILES array using $.post

I am attempting to submit a form via jQuery. My form contains fields and a file that must be uploaded. It is of type ENCTYPE="multipart/form-data".

I can receive all my field values using: post = $('#myForm').serialize(); But how do I receive the $_FILES array? I need this to process the uploaded file.

Is this possible using jQuery, and if so how? Or do I need to use a special upload plugin for jQuery?

like image 503
user49226 Avatar asked Feb 15 '09 21:02

user49226


2 Answers

jquery form is the best way to do it, you can add it to any normal form,

<form method="post" action="URL">
<input type="file" name="file">
<input type="text" name"text">
<input type="submit"> 
</form>

<script type="text/javascript">
$(document).ready(function() { 
  $(form).ajaxForm();
})
</script>

will work as expected, but with ajax.

http://malsup.com/jquery/form/#code-samples

like image 181
Bruce Aldridge Avatar answered Oct 13 '22 00:10

Bruce Aldridge


You cannot upload files through javascript.

Check out this related question:
Is it possible to use Ajax to do file upload?

Essentially, the two most popular ways of "faking" AJAX for file uploads is using a Flash plugin such as SWFUpload or submitting the form to a hidden iframe that processes the request.

like image 30
Paolo Bergantino Avatar answered Oct 13 '22 01:10

Paolo Bergantino