传斑点通过Ajax来生成一个文件 [英] Pass Blob through ajax to generate a file

查看:121
本文介绍了传斑点通过Ajax来生成一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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调用:

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

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:我是新手,有斑点和Ajax。 先谢谢了。

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

推荐答案

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

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

这篇关于传斑点通过Ajax来生成一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆