Skip to content Skip to sidebar Skip to footer

Recorderjs Uploading Recorded Blob Via Ajax

I'm using Matt Diamond's recorder.js to navigate the HTML5 audio API, and feel this question probably has an apparent answer, but i'm unable to find any specific documentation. Que

Solution 1:

If you have the blob you'll need to turn it into a url and run the url through an ajax call.

// might be nice to set up a boolean somewhere if you have a handler objectobject = newObject();
object.sendToServer = true;

// You can create a callback and set it in the config object.var config = {
   callback : myCallback
}

// in the callback, send the blob to the server if you set the property to truefunctionmyCallback(blob){
   if( object.sendToServer ){

     // create an object url// Matt actually uses this line when he creates Recorder.forceDownload()var url = (window.URL || window.webkitURL).createObjectURL(blob);

     // create a new request and send it via the objectUrlvar request = new XMLHttpRequest();
     request.open("GET", url, true);
     request.responseType = "blob";
     request.onload = function(){
       // send the blob somewhere else or handle it here// use request.response
     }
     request.send();
   }
}

// very important! run the following exportWAV method to trigger the callback
rec.exportWAV();

Let me know if this works.. haven't tested it but it should work. Cheers!

Solution 2:

I also spent many hours trying to achieve what you are trying to do here. I was able to successfully upload the audio blob data only after implementing a FileReader and calling readAsDataURL() to convert the blob to a data: URL representing the file's data (check out MDN FileReader). Also you must POST, not GET the FormData. Here's a scoped snippet of my working code. Enjoy!

functionuploadAudioFromBlob(assetID, blob)
{
    var reader = newFileReader();

    // this is triggered once the blob is read and readAsDataURL returns
    reader.onload = function (event)
    {
        var formData = newFormData();
        formData.append('assetID', assetID);
        formData.append('audio', event.target.result);
        $.ajax({
            type: 'POST'
            , url: 'MyMvcController/MyUploadAudioMethod'
            , data: formData
            , processData: false
            , contentType: false
            , dataType: 'json'
            , cache: false
            , success: function (json)
            {
                if (json.Success)
                {
                    // do successful audio upload stuff
                }
                else
                {
                    // handle audio upload failure reported// back from server (I have a json.Error.Msg)
                }
            }
            , error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error! '+ textStatus + ' - ' + errorThrown + '\n\n' + jqXHR.responseText);
                // handle audio upload failure
            }
        });
    }
    reader.readAsDataURL(blob);
}

Solution 3:

@jeff Skee's answer really helped but I couldn't grasps it at first, so i made something simpler with this little javascript function. Function parameters @blob : Blob file to send to server @url : server side code url e.g. upload.php @name : File index to reference at the server side file array

jQuery ajax function

functionsendToServer(blob,url,name='audio'){
var formData = newFormData();
    formData.append(name,blob);
    $.ajax({
      url:url,
      type:'post',      
      data: formData,
      contentType:false,
      processData:false,
      cache:false,
      success: function(data){
        console.log(data);
      }
    });  }

Server side code (upload.php)

$input = $_FILES['audio']['tmp_name'];
$output = time().'.wav';
if(move_uploaded_file($input, $output))
    exit('Audio file Uploaded');

/*Display the file array if upload failed*/exit(print_r($_FILES));

Solution 4:

Both solutions above use jQuery and $.ajax()

Here's a native XMLHttpRequest solution. Just run this code wherever you have access to the blob element:

var xhr=newXMLHttpRequest();
xhr.onload=function(e) {
  if(this.readyState === 4) {
      console.log("Server returned: ",e.target.responseText);
  }
};
var fd=newFormData();
fd.append("audio_data",blob, "filename");
xhr.open("POST","upload.php",true);
xhr.send(fd);

Server-side, upload.php is as simple as:

$input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file$output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea//move the file from temp name to local folder using $output name
move_uploaded_file($input, $output)

source | live demo

Post a Comment for "Recorderjs Uploading Recorded Blob Via Ajax"