找到你要的答案

Q:Pass Blob through ajax to generate a file

Q:通过BLOB通过Ajax生成文件

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.

我试图捕捉audiorecorder(https://github。com/cwilso/audiorecorder)和通过Ajax的PHP文件发送的斑点,将收到的blob内容和创建文件(在这种情况下的波形文件)。

Ajax调用:

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!");
        }
      });
    }); 

我的PHP文件(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。

answer1: 回答1:

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);
}
?>

尝试将文件作为表单数据上传

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);
}
?>
jquery  ajax  file-upload  blob  wave