views:

202

answers:

2

hi ,

i need to post a form which includes personal details and i need to upload a attachment file also and i need to achieve this using ajax ,

how can i upload a file using ajax , i need to post the personal detail as well as upload a file at the same time using ajax , how can i do this?

+2  A: 

You can't upload files with AJAX because you can't do multi-part encoded requests - that's a limitation of the XMLHttpRequest API.

Workarounds for this typically involve using an IFRAME that is the target of a form that handles the actual file upload as a separate sub-process to the overall form. The response loaded in the IFRAME then communicates back to the DOM in the parent/calling page with information about the uploaded file.

Peter Bailey
A: 

If you add a hidden iframe on the page

<iframe name="iframe1" ... />

And the change the target on your form to post to the iframe

<Form traget="iframe1" ...>...</form>

You can upload and post the data to the page. You can also use javascript (JQuery) to get hook into the events of the iframe.

To get a upload progress meter, use the php uploadprogress module. It works great! http://pecl.php.net/package/uploadprogress and http://bluga.net/projects/uploadProgressMeter/

Here is a link i found that may help: http://www.finalwebsites.com/forums/topic/php-ajax-upload-example

aquillin