Upload API via Ajax

Ritesh,
Can you send us the config of the demo site. So we can compare our plugins and versions to your site.

Debug info of our demo site:

Home URL http://demo.rtcamp.com/rtmedia
Site URL http://demo.rtcamp.com/rtmedia
PHP 5.5.10-1+deb.sury.org~precise+1
MYSQL 5.6.15
WordPress 3.9
BuddyPress 2.0.1
rtMedia 3.6.14
OS Linux
Imagick ImageMagick 6.6.9-7 2014-03-06 Q16 www.imagemagick.org
GD 2.1.1-dev
[php.ini] post_max_size 200M
[php.ini] upload_max_filesize 200M
[php.ini] memory_limit 256M
Installed Plugins Akismet by Automattic version 3.0.0,
bbPress by The bbPress Community version 2.5.3,
rtMedia Instagram by rtCamp version 2.1.4,
rtMedia Photo Tagging by rtCamp version 2.2.4,
rtMedia for WordPress, BuddyPress and bbPress by rtCamp version 3.6.14,
BuddyPress by The BuddyPress Community version 2.0.1,
Contact Form 7 by Takayuki Miyoshi version 3.8,
Google Analytics for WordPress by Joost de Valk version 4.3.5,
Gravity Forms by rocketgenius version 1.7.9,
IJM Theme Switcher Bar by Iain J McCallum version 2.0,
MailChimp Newsletter Widget by Matthew Poer version 1.0,
MailChimp Widget by James Lafferty version 0.8.12,
myCRED by Gabriel S Merovingi version 1.4.4,
Ninja Forms by The WP Ninjas version 2.6.2,
Qr Code Adv by Branimir Ivanov version 1,
Quick Page/Post Redirect Plugin by Don Fischer version 5.0.6,
Rewrite Rules Inspector by Daniel Bachhuber, Automattic version 1.2.1,
rtAccount Login by $trik3r version 1.01,
Google Analytics plugin to track page scrolling by rtCamp version 1.0.0,
rtAntiSpam by rtcamp version 1.0,
rtMedia Pro by rtCamp version 2.3.1,
rtMedia Widgets by rtCamp version 1.0,
rtSocial by rtCamp, rahul286, rutwick, saurabhshukla, HarishChaudhari, faishal, 5um17, JoshuaAbenazer version 2.1.15,
WooCommerce by WooThemes version 2.1.8,
WordPress Importer by wordpressdotorg version 0.6.1
Theme Name InspireBook
Theme Version 1.2.2
Author URL https://rtcamp.com/
Template Overrides themes/pt-rtmedia/rtmedia/media/album-gallery-item.php,
themes/pt-rtmedia/rtmedia/media/album-gallery.php,
themes/pt-rtmedia/rtmedia/media/album-single-edit.php,
themes/pt-rtmedia/rtmedia/media/media-gallery-item.php,
themes/pt-rtmedia/rtmedia/media/media-gallery.php,
themes/pt-rtmedia/rtmedia/media/media-single-edit.php,
themes/pt-rtmedia/rtmedia/media/media-single.php,
plugins/buddypress-media/templates/media/uploader.php

Can you create an account for me on your site so that I can check the issue. My ID is [email protected]

Ritesh,
I just sent credentials. I see we’ve got different PHP versions running. I set you up as an admin.

I’m updating to PHP 5.4, but I found an issue that may be helpful. In the console, I’m now getting an HTML warning message:


Warning: file_put_contents(/home/content/62/10656162/tmp/test_from_console.image/jpg) [function.file-put-contents]: failed to open stream: No such file or directory in /home/content/62/10656162/html/dev/wp-content/plugins/buddypress-media/app/main/controllers/api/RTMediaJsonApi.php on line 962

Hi Justyn,
It is trying to locate directory ‘/home/content/62/10656162/tmp/test_from_console.image/jpg’ which is not exist. Change image type to image/jpg to jpg.

Hey Ritesh,
As I noted back a few posts ago, when I remove the ‘image/’, I get the status_Code:140006, image type missing response.

I think the API is failing to remove the ‘image/’ from the file name prior to storing the image, which is causing the upload to fail.

Hi Justyn,
Can you send SSH / FTP credentials to [email protected] so that I can debug the issue.

Ritesh,
We will not be allowing FTP access to our site. We can give you limited SSH credentials.

Please look into your code to ensure that “image/” is being stripped when you parse the string.

@don-cornforthacgsystem-com,
I need access to the code that is running on your site so that I can look into the issue and can debug it as it is work perfectly fine on our demo site.

Hey Ritesh,
We just sent you those credentials.

Justyn

Hi Justyn,

As per our conversation over mail, this issue has been solved.

Hello,

Guys, Can you please share the code, How you have been fixed this ? I am also getting the upload status ‘media updated’ and it’s add the record in the db successfully but not able to uplaod the image. It shows me blank image in the profile media tab.

Please help me to solve out this.

Here is my code :

    $img = $plugin_path.'/includes/'.$m_id.'.jpg';

    $base64img = 'data:'.mime_content_type($img).';base64,'.base64_encode(file_get_contents($img));

   $url = $base_url.'/wp-admin/admin-ajax.php';

   $(document).ready(function(){
    var form_data = {};

    form_data['action'] = 'rtmedia_api';
    form_data['method'] = 'rtmedia_upload_media';
    form_data['token'] = '2704b21c7c7fd826c4f175c530b0d5db6630e3a4';
    form_data['rtmedia_file'] = '<?php echo $base64img; ?>';
    form_data['title'] = 'test image';
    form_data['context'] = 'profile';
    form_data['image_type'] = 'image/jpeg';
    
    $.ajax({
        url:'<?php echo $url; ?>',
        type:'Post',
        data:form_data,    
        success:function(response){
            alert(response);
            return false;
        }
    });    
})

Thanks !

Hi @kuldeep_kbs,

Please make sure you have encoded correct file, check with some other file to make sure whether the file is correct or not. If record in database added successfully than it mush show the image.

Check this pastebin link for sample base64 encoded image: http://pastebin.com/RtqTL2A3

Required parameters to upload media are as following:

action: "rtmedia_api"
method: "rtmedia_upload_media"
token: "access-token-here"
context: "profile"
image_type: "jpeg"
title: "test api upload"
rtmedia_file: "base64 encoded file here"

Hi @riteshpatel , I am trying to upload a image into specific album, but it is uploading to the user profile only, Please check the below code and correct me that what parameter API will accept to upload the media to user’s album, group album.

form_data['action'] = 'rtmedia_api'; form_data['method'] = 'rtmedia_upload_media'; form_data['token'] = 'token';         form_data['title'] = 'title';        form_data['context'] = 'profile';   or group in case if it's a group album           form_data['context_id'] = 'user_id';  or group_id right ?                          form_data['album_id'] = 'ablbum_id';   form_data['image_type'] = 'jpeg';    form_data['rtmedia_file'] = 'base_64_string'; 

Thanks!

Hi @kuldeep_kbs,

It was a minor issue in JSON API which we have just fixed. You can check it here: https://github.com/rtCamp/rtMedia/commit/da951e7

If you are developer, and are familiar with coding, you can make changes in plugin file for time being and make it work, it will be officially fixed in next update of rtMedia.

Hi @riteshpatel, Thanks for you update. Can we use this API through curl ?

Hi @kuldeep_kbs As we can make a post request with CURL, so I guess you can use API through CURL but I am not sure, I haven’t checked that.

Hi @riteshpatel,

Do you have any example to convert jQuery style parameters to AngularJS?

Thanks