通过Ajax传递Blob以生成文件

我正在尝试捕获audiorecorder(https://github.com/cwilso/AudioRecorder)并通过Ajax向Blob发送一个php文件,该文件将接收Blob内容并创建文件(在这种情况下为wave文件)。

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

}?>

PS:我是blob和ajax的新手。提前致谢。

回答:

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

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

}

?>

以上是 通过Ajax传递Blob以生成文件 的全部内容, 来源链接: utcz.com/qa/408367.html

回到顶部