I have a video upload form inside a page with another form. I want to submit the upload form by ajax and receive the response from Bits on the Run to the same page (and put the data in the second form).
How can I use the /videos/create API call with ajax?
You should never perform API calls through javascript, as that way it will be easy for any user to snatch your key and secret and take over your account.
I recommend that you take a look at the source of our wordpress plugin, as in that plugin it is possible to upload a video without leaving the page: http://www.longtailvideo.com/support/bits-on-the-run/15959/using-our-wordpress-plugin
I've tried using jQuery post, but it doesn't work. The code I use for the jQuery post is : bc.. <?php require_once('botr/init_api.php');
# Do the API call to build an upload URL. # The 'token' MUST be the last parameter for upload progress to work. $response = $botr_api->call('/videos/create'); $token = $response['link']['query']['token']; if ($response['status'] == 'error') { die(print_r($response)); } $url = 'http://'.$response['link']['address'].$response['link']['path'];
# Print the page. All identifiers inside the form are used to display the upload progress. ?> <script type="text/javascript"> $(document).ready(function() {
$('#uploadForm').submit(function() { var url = $(this).attr('action'); alert(url); var dataToBeSent = $(this).serialize(); alert(dataToBeSent); $.post(url, dataToBeSent, function(data, textStatus) { //data contains the JSON object //textStatus contains the status: success, error, etc alert("textStatus"); alert(textStatus); }, "json");
<p class="hint"> You can upload any video format (WMV, AVI, MP4, MOV, FLV, ...) </p> <button type="submit" id="uploadButton">Upload</button> </fieldset> </form>
When I submit the form the alert of the url shows http://upload.bitsontherun.com/v1/videos/upload and the alert of dataToBeSent shows key=XXX&api_format=json&uploadToken=YYY&token=YYY
but than nothing happens. (When I upload a video by posting a form, like explained here : http://www.longtailvideo.com/support/bits-on-the-run/15984/upload-videos-within-your-cms it works fine)
Did you get it working? I wanted a simular thing (probably others to) and eventually got it working..
I've a form in my cms where the bits on the run video is just a small part of the dataset. After the user selects a file, I want the file uploaded and posted to bits on the run. In the mean time the user should be able to fill out the rest of the form. When the uploading is done, the video key should be inserted in a hidden field of my form, without the page reloading. If the video is uploaded and the user done with filling out the other fields, he can save everything to the local database, including the reference to the video on the bits on the run server.
I looked at the code from the wordpress plugin which indeed does something simular (thnx for pointing that out!). The only problem is that it uses a prototype plugin to upload a multipart-form via an iframe because apperently that's not something you can do through ajax.
Fortunately their is a jquery plugin that does the job http://www.jainaewen.com/files/javascript/jquery/iframe-post-form.html#demonstrations
Include it and you can do something like: bc.. $_('#uploadForm').iframePostForm ({ complete : function (response) { update_latest_botr_thumb(); } });
Unfortunately the response is empty due to cross domain restrictions so I used the same trick as in the wordpress plugin: Making a ajax call to get a list of all videos that where recently uploaded, including the one that i just uploaded :).
bc.. function update_latest_botr_thumb(){
//get JSON feed from Bits on the Run via php $_.getJSON( 'index.php?option=com_survey&view=videos&format=json', function(data){ var hItem = '';
//render html, no thumb is available while processing if(data.videos[0].status=='processing'){ hItem = '<div class="item '+data.videos[0].key+'"><span class="processing">processing</span><span>'+data.videos[0].title+'</span></div>';
//keep checking if processing is done setTimeout('update_latest_botr_thumb()',5000); } else if(data.videos[0].status=='ready'){ hItem = '<div class="item '+data.videos[0].key+'"><img src="http://content.bitsontherun.com/thumbs/'+data.videos[0].key+'-120.jpg" width="120" /><span>'+data.videos[0].title+'</span></div>'; }
//insert html when it's the first time if($_('.'+data.videos[0].key).length==0){ $_('.botr_videos').prepend(hItem);
} //update the html when the video is processed else if(data.videos[0].status=='ready'){ $_('.botr_videos .item').eq(0).replaceWith(hItem); }
I'm using Bits-on-the-run in my Drupal 7 project. By using the BOTR widget it is uploading the video file with a success message. Where as i tried to upload the video from another form of custom form. I'm able to form the POST_URL and it is able to call the submit function but i'm getting the key missing(parameter) error.