2013-03-28 198 views
2

我正在使用https://github.com/cwilso/AudioRecorder並且除了獲取blob並將其發送到服務器以外,一切正常。我有下面的表單數據發送到服務器。基本上,在客戶端生成一個wav文件,然後它存儲在一個blob中,我想找出一種方法來獲取該blob的內容。Javascript Blob保存到變量

$('#submit').click(function(){ 
var formData = new FormData($('#add_slide').get(0)); 
var jContent = $("#main_container"); 
//console.log(formData); 

if($('#audio_file').val().length==0) 
{ 

    var blob_url = $('#blob_url').val(); 
    if($('#blob_url').val().length==0) 
    { 
     alert('Recording Could not be found. Please try again'); 
     return false; 
    }else{ 
     console.log(formData); 
    } 
    //return false; 
}else{ 
    var ext = $('#audio_file').val().split('.').pop().toLowerCase(); 
    if(ext!== 'wav') { 
     alert('Invalid File. Please use a file with extension WAV!'); 
     return false; 
    } 
} 

$.ajax({ 
    url: 'lec_add_slide.php', //server script to process data 
    type: 'POST', 
    xhr: function() { // custom xhr 
     myXhr = $.ajaxSettings.xhr(); 
     if(myXhr.upload){ // check if upload property exists 
      myXhr.upload.addEventListener('progress',progressHandlingFunction, false);  // for handling the progress of the upload 
      //console.log('OK'); 
     }else{ 
      //console.log('NOT'); 
     } 
     return myXhr; 
    }, 
    //Ajax events 
    beforeSend: function(){ 
     $('#loadingModal').modal('show'); 
    }, 
    success: function (data) { 
     jContent.html(data); 
     $('#loadingModal').modal('hide'); 
    }, 
    error: function(){ 
     console.log('error'); 
    }, 
    // Form data 
    data: formData, 
    //Options to tell JQuery not to process data or worry about content-type 
    cache: false, 
    contentType: false, 
    processData: false 
}); 
}); 
function progressHandlingFunction(e){ 
    if(e.lengthComputable){ 
    $('#bar-progress-mp3').css('width',((e.loaded/e.total)*100)+'%'); 
} 
} 

如果我使用普通的文件輸入發送常規文件,一切正常。我已經把blob url放在一個隱藏的輸入字段中,我也試過blob.slice(),但到達服務器的只是對象Blob。 有誰知道如何獲取blob URL的內容並將其發送到服務器?

任何幫助表示讚賞。

回答

2

您可以將BLOB添加到FormData,像formData.append('thename', theblob);

+0

我必須是一個白癡。我一整天都在這裏。我嘗試了你之前的建議,但我認爲它沒有奏效。所以看到你的評論後,我運行wireshark,捕獲流量,並看到該文件確實要到服務器。 PHP在$ _FILES中接收它,並且我也有錯誤的var名稱。謝謝 – Nic

+0

@Nic歡迎 – Musa

+0

謝謝,節省了很多時間.. –