[wp-hackers] Handle Image Upload via jQuery .post?

Blair Williams blair at caseproof.com
Tue Nov 5 03:36:26 UTC 2013


File uploads aren’t possible with AJAX … they only work with actual form posts.

If you google around you’ll find that people have all kinds of different techniques around this limitation. One that I remember from years back was a guy who embedded a conventional form within an iFrame on his page … submitted it and used the js in the parent frame to make it appear dynamic :) ..

But I just stick to standard form posts for the image uploads … then ajax for everything else.

Blair

From: BenderisGreat BenderisGreat
Reply: wp-hackers at lists.automattic.com wp-hackers at lists.automattic.com
Date: November 4, 2013 at 8:22:06 PM
To: wp-hackers at lists.automattic.com wp-hackers at lists.automattic.com
Subject:  [wp-hackers] Handle Image Upload via jQuery .post?  
converting a form to ajax, and I currently have file uploads being handles  
like so:  

function insert_attachment($file_handler,$post_id,$setthumb='false') {  
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){return  
__return_false(); }  
require_once(ABSPATH . "wp-admin" . '/includes/image.php');  
require_once(ABSPATH . "wp-admin" . '/includes/file.php');  
require_once(ABSPATH . "wp-admin" . '/includes/media.php');  
$attach_id = media_handle_upload( $file_handler, $post_id );  

if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);  
return $attach_id;  

}  

Then in the form, I am using:  


foreach ($_FILES as $file => $array)  
{  
$newupload = insert_attachment($file,$post_id);  
}  

This returns uploads the image and returns the attachment id for placement  
in a db table. After converting to ajax, this function no longer works.  

Any possible way to run the insert_attachment function on click only, and  
then run the ajax post submit? Or obviously the better solution would be to  
handle it all from the action call function.  






--  
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Handle-Image-Upload-via-jQuery-post-tp42717.html  
Sent from the Wordpress Hackers mailing list archive at Nabble.com.  
_______________________________________________  
wp-hackers mailing list  
wp-hackers at lists.automattic.com  
http://lists.automattic.com/mailman/listinfo/wp-hackers  


More information about the wp-hackers mailing list