Discussion:
Handle Image Upload via jQuery .post?
BenderisGreat
2013-11-05 03:21:58 UTC
Permalink
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.
Blair Williams
2013-11-05 03:36:26 UTC
Permalink
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-***@lists.automattic.com wp-***@lists.automattic.com
Date: November 4, 2013 at 8:22:06 PM
To: wp-***@lists.automattic.com wp-***@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-***@lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers

Loading...