o使用其blob url在本地上传音频文件

时间:2015-03-31 17:51:35

标签: javascript php xmlhttprequest

我有一项记录音频并将其存储在服务器上的任务。现在,我正在使用本地python服务器来运行代码。我正在获取音频文件的blob url,但是当我调用以下javascript函数来存储它时,它给出了错误: -

  function upload(blob) {
  var xhr;
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xhr=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xhr=new ActiveXObject("Microsoft.XMLHTTP");
  }

  xhr.open('GET', 'upload.php',true);
  xhr.onload = function(e) {  };

  // Listen to the upload progress.
  var progressBar = document.querySelector('progress');

  var data= new FormData(this);
  data.append('file',blob);
  //xhr.setRequestHeader("Content-type","Blob");
  xhr.send(data);
};

我在服务器端的upload.php

   <?php

    if(isset($_FILES["file"])) {

        $f = $_FILES["file"];
        $dir = "data";

        if(!file_exists($dir))
            mkdir($dir);

        foreach($f["name"] as $k => $name) {

            $file = $dir."/".$name;

            if($f["error"][$k] == 0 && move_uploaded_file($f["tmp_name"][$k], $file)) {
                $uploaded[] = $file;
            }

        }

        die(json_encode($uploaded));        
    }

?>

0 个答案:

没有答案