dsigg21445 2015-10-28 17:05
浏览 354
已采纳

将blob文件保存到服务器

I am trying to saeve recorded file to the server. For recording purpose I am using demos recorder

At the end of recording it gives me a blob-link to the recorded file. So After googling a bit I found that I can use that bob url to save it.
Here is the link that talks about saving blobs. After that I am trynig to get it and download to server.

1- I get the link to blob file

var data = document.getElementById("save").href

After that I am using js code in my index.html file to send
blob url to php code.
JS code

<script>
function saveAudio(){
var req = null;
var url = "savefile.php";
var data = document.getElementById("save").href.toString();// document.getElementById("save").innerHTML;// = xhttp.responseText;;   // you have to check how to get the data from your saveAudio() method
window.alert(data);

(window.XMLHttpRequest) ? req = new XMLHttpRequest() : (window.ActiveXObject) ? req = new ActiveXObject("Microsoft.XMLHTTP") : req = false; 
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "multipart/form-data");

if(data != null) //&& data != "")
{
  req.setRequestHeader("Content-length", data.length);
  req.send(data);
}}


</script>

PHP code

<?php

$save_folder = dirname(__FILE__) ."/js";
if(! file_exists($save_folder)) {
    if(! mkdir($save_folder)) {
        die("failed to create save folder $save_folder");
    }
}

$key = 'filename';
$tmp_name = $_FILES["audiofile"]["tmp_name"];
$upload_name = $_FILES["audiofile"]["name"];
$type = $_FILES["audiofile"]["type"];
$filename = "$save_folder/$upload_name";
$saved = 0;
if(($type == 'audio/x-wav' || $type == 'application/octet-stream') && preg_match('/^[a-zA-Z0-9_\-]+\.wav$/', $upload_name) ) {

    $saved = move_uploaded_file($tmp_name, $filename) ? 1 : 0;
}

//name is needed to send in the php file

?>

I get 2 errors while compiling in browser
1-refused to set unsafe header "Content-length".
2-POST savefile.php 500

I suppose that there is something wrong with php file!
How canI handle these errors and accomplish uploading task?
Is there any open source which allows direct uploading blob-url to server without using php?
I appreciate any help and suggestion!

  • 写回答

1条回答 默认 最新

  • douya2433 2015-10-28 18:09
    关注

    Try removing the line: req.setRequestHeader("Content-length", data.length);

    XMLHttpRequest isn't allowed to set these headers because that would be a security vulnerability. The 500 is most likely a result of the request failure.

    You can read about XMLHttpReqest here http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method

    Here are some other threads, same issue:

    Pass Blob through ajax to generate a file

    How can javascript upload a blob?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)