音频blob通过ajax发布到PHP返回NULL

时间:2017-05-05 03:45:19

标签: javascript php ajax recorder.js

我正在使用recorder.js。我的目标是将录制内容上传到服务器,似乎音频blob正在发布到服务器。但是我继续为$ _FILES返回null [" file"] [" tmp_name"];

使用Javascript:

function uploadAudio(){
audioRecorder.stop();
audioRecorder.exportWAV(function(audio) {
var fd = new FormData();
fd.append('filename', 'test.wav');
fd.append('data', blob);
$.ajax({
    type: 'POST',
    url: 'testthing.php',
    data: fd,
    processData: false,
    contentType: false
}).done(function(data) {
       console.log(data);
});
});
}

PHP:

$res="recordings/";
$yo = $_FILES["file"]["tmp_name"];
rename($yo,$res.'test.wav');

这是我在firefox中使用firebug发布的标题数据所得到的:

-----------------------------2600221228510
Content-Disposition: form-data; name="filename"

test.wav
-----------------------------2600221228510
Content-Disposition: form-data; name="data"; filename="blob"
Content-Type: audio/wav

1 个答案:

答案 0 :(得分:0)

感谢Pass Blob through ajax to generate a file我能够让它发挥作用

<强>的javascript:

audioRecorder.exportWAV(function(blob) {

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

      var filename = "test.wav";
      var data = new FormData();
      data.append('file', blob);

      $.ajax({
        url :  "testthing.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'], "recordings/" . $fname);
}
?>