2015-06-16 46 views
1

我正在使用recorder.js在我的網頁上錄製音頻,它創建了一個代表wav文件的blob。我想將這個blob文件發送到我的c#服務器代碼,以便我可以將其上傳到我的azure存儲。我在這方面做了很多研究,只能找到將blob發送到php的結果。到目前爲止,這是唯一的代碼,我能找到送斑點到後面的代碼:如何將Javascript中的Blob文件傳遞給ASP.net中的服務器代碼

function upload(blob) { 
    var xhr = new XMLHttpRequest(); 
    xhr.open('POST', './addRecord.aspx', true); 
    xhr.onload = function (e) { 
     var result = e.target.result; 
    }; 
    xhr.send(blob); 
} 

然後在後面的Page_Load功能我addRecord.aspx代碼,我有:

Request.SaveAs(Server.MapPath("file.wav"), false); 

當我檢查我保存的文件,我無法打開任何文件。該文件似乎已損壞,所以我認爲我無法成功傳遞文件。我也聽說使用ajax很容易做到,但我不知道如何實現它。我很樂意就如何做到這一點提出任何想法。

+0

如果這個工程的一個PHP腳本,您的服務器端代碼是錯誤的。 – CodeCaster

+1

可能重複[如何異步上傳文件?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) – Knelis

回答

0

您可以使用此技術:

的Base-64

您可以閱讀使用JavaScript的FileReader API從WAV文件中的內容,接下來編碼的二進制內容到BASE-64通過這像字符串字符。

接下來的是一個簡單的例子:

<input type="file" id="files" name="file" /> 

<script> 
    function handleFileSelect(evt) { 
     var files = evt.target.files; // FileList object 

     // Loop through the FileList and render image files as thumbnails. 
     for (var i = 0, f; f = files[i]; i++) { 
      // Only process audio files. 
      if (!f.type.match('audio.*')) { 
       continue; 
      } 

      var reader = new FileReader(); 

      // Closure to capture the file information. 
      reader.onload = (function(theFile) { 
       return function(e) { 
        var xhr = new XMLHttpRequest(); 
        xhr.open('POST', './addRecord.aspx/MyPostMethod', true); 
        xhr.onload = function (e) { 
         var result = e.target.result; 
        }; 
        xhr.send('{"blob":"' + e.target.result + '"}'); 
       }; 
      })(f); 

      // Read in the image file as a data URL. 
      reader.readAsDataURL(f); 
     } 
    } 

    document.getElementById('files').addEventListener('change', handleFileSelect, false); 
</script> 

結果是得到這樣的Base-64編碼

我希望這可以幫助您

相關問題