26

I'm trying to capture audiorecorder (https://github.com/cwilso/AudioRecorder) and send the blob through Ajax a php file, which will receive the blob content and create the file(the wave file in this case).

Ajax call:

audioRecorder.exportWAV(function(blob) {
      var url = (window.URL || window.webkitURL).createObjectURL(blob);
      console.log(url);
      var filename = <?php echo $filename;?>;
      $.ajaxFileUpload({
        url :  "lib/vocal_render.php",
        secureuri      :false,
        dataType : blob.type,
        data: blob,
        success: function(data, status) {
          if(data.status != 'error')
            alert("boa!");
        }
      });
    }); 

and my php file (vocal_render.php):

<?php 

if(!empty($_POST)){
    $data = implode($_POST); //transforms the char array with the blob url to a string
    $fname = "11" . ".wav";

    $file = fopen("../ext/wav/testes/" .$fname, 'w');
    fwrite($file, $data);
    fclose($file);
}?>

P.S:I'm newbie with blobs and ajax. Thanks in advance.

João Correia
  • 275
  • 1
  • 3
  • 7

3 Answers3

38

Try uploading the file as form data

audioRecorder.exportWAV(function(blob) {

      var url = (window.URL || window.webkitURL).createObjectURL(blob);
      console.log(url);

      var filename = <?php echo $filename;?>;
      var data = new FormData();
      data.append('file', blob);

      $.ajax({
        url :  "lib/vocal_render.php",
        type: 'POST',
        data: data,
        contentType: false,
        processData: false,
        success: function(data) {
          alert("boa!");
        },    
        error: function() {
          alert("not so boa!");
        }
      });
}); 

.

<?php 

if(isset($_FILES['file']) and !$_FILES['file']['error']){
    $fname = "11" . ".wav";

    move_uploaded_file($_FILES['file']['tmp_name'], "../ext/wav/testes/" . $fname);
}
?>
akirk
  • 6,757
  • 2
  • 34
  • 57
Musa
  • 96,336
  • 17
  • 118
  • 137
  • 1
    Does it also work without "FormData"? So that it is possible to send it with an image MIME type? – Benny Code Jan 31 '14 at 15:50
  • 1
    @BennyNeugebauer you can send it directly as a File or a Blob but you'd need to process it differently on the server. – Musa Jan 31 '14 at 15:53
  • 1
    I was searching for a solution to this all day, tried a bunch of different methods, but this is the first one to work! Thanks! – Jeff May 05 '17 at 05:14
2

According to the documentation, by using XMLHttpRequest.send() you can use the Blob object directly.

var blob = new Blob(chunks, { 'type' : 'audio/webm' });
var xhr = new XMLHttpRequest();
xhr.open('POST', '/speech', true);
xhr.onload = function(e) {
  console.log('Sent');
};
xhr.send(blob);

I've tried this and it works like a charm.

juan.facorro
  • 9,791
  • 2
  • 33
  • 41
0

To make an AJAX call and generate a CSV file for download in Laravel blade

<button type="button" class="btn btn-primary" id="download-btn">Download</button>
<script>
    $(document).on('click', '#download-btn', function(e) {
        e.preventDefault();
        $.ajax({
            "url": "{{ config('app.api_link').'/import/lc-register-report?download=yes' }}",
            "type": "POST",
            xhrFields:{
                responseType: 'blob'
            },
            data: { },
            headers: {
                'Authorization': "Bearer {{ Session::get('authorization_token') }}"
            },
            success: function(response) 
            {
                var data = new Blob([response], {type: 'application/vnd.ms-excel'}); // csv = 'text/csv'
                var csvURL = window.URL.createObjectURL(data);
                const tempLink = document.createElement('a');
                tempLink.href = csvURL;
                tempLink.setAttribute('download', 'transaction-report.xls'); // csv = '.csv'
                tempLink.click();
            }
        });
    });
</script>