Pass Blob through ajax to generate a file

前端 未结 2 556
青春惊慌失措
青春惊慌失措 2020-11-27 04:32

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

相关标签:
2条回答
  • 2020-11-27 05:22

    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);
    }
    ?>
    
    0 讨论(0)
  • 2020-11-27 05:23

    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.

    0 讨论(0)
提交回复
热议问题