鍍金池/ 問答/HTML5  HTML/ 【求助】怎么將blob 對象 用$.post()的方式傳到node中

【求助】怎么將blob 對象 用$.post()的方式傳到node中

1.最近在嘗試用node.js 做語音識別 用的是百度的api 現(xiàn)在碰到了解決不了的問題 錄音的庫 返回的是一個blob
我做了各個方面的嘗試 我得需求是這樣的 通過web錄音然后獲取到blob 對象 然后通過$.post()的方式將這個blob傳入node中 然后再通過node寫入一個wav的文件
2.$.post()中的data 不能傳入對象
3.blob 序列化 通過fileReader 序列化后 過長
4.網(wǎng)上查到了一個blob-to-buffer 的庫 要求傳過來的是一個blob對象 所以現(xiàn)在希望最好是能直接將blob對象傳到后臺

代碼如下:
后臺創(chuàng)建的一個錄音 blob對象

encodeWAV: function () {  
                var sampleRate = Math.min(this.inputSampleRate, this.outputSampleRate);  
                var sampleBits = Math.min(this.inputSampleBits, this.oututSampleBits);  
                var bytes = this.compress();  
                var dataLength = bytes.length * (sampleBits / 8);  
                var buffer = new ArrayBuffer(44 + dataLength);  
                var data = new DataView(buffer);  
  
                var channelCount = 1;//單聲道  
                var offset = 0;  
  
                var writeString = function (str) {  
                    for (var i = 0; i < str.length; i++) {  
                        data.setUint8(offset + i, str.charCodeAt(i));  
                    }  
                }  
  
                // 資源交換文件標識符   
                writeString('RIFF'); offset += 4;  
                // 下個地址開始到文件尾總字節(jié)數(shù),即文件大小-8   
                data.setUint32(offset, 36 + dataLength, true); offset += 4;  
                // WAV文件標志  
                writeString('WAVE'); offset += 4;  
                // 波形格式標志   
                writeString('fmt '); offset += 4;  
                // 過濾字節(jié),一般為 0x10 = 16   
                data.setUint32(offset, 16, true); offset += 4;  
                // 格式類別 (PCM形式采樣數(shù)據(jù))   
                data.setUint16(offset, 1, true); offset += 2;  
                // 通道數(shù)   
                data.setUint16(offset, channelCount, true); offset += 2;  
                // 采樣率,每秒樣本數(shù),表示每個通道的播放速度   
                data.setUint32(offset, sampleRate, true); offset += 4;  
                // 波形數(shù)據(jù)傳輸率 (每秒平均字節(jié)數(shù)) 單聲道×每秒數(shù)據(jù)位數(shù)×每樣本數(shù)據(jù)位/8   
                data.setUint32(offset, channelCount * sampleRate * (sampleBits / 8), true); offset += 4;  
                // 快數(shù)據(jù)調整數(shù) 采樣一次占用字節(jié)數(shù) 單聲道×每樣本的數(shù)據(jù)位數(shù)/8   
                data.setUint16(offset, channelCount * (sampleBits / 8), true); offset += 2;  
                // 每樣本數(shù)據(jù)位數(shù)   
                data.setUint16(offset, sampleBits, true); offset += 2;  
                // 數(shù)據(jù)標識符   
                writeString('data'); offset += 4;  
                // 采樣數(shù)據(jù)總數(shù),即數(shù)據(jù)總大小-44   
                data.setUint32(offset, dataLength, true); offset += 4;  
                // 寫入采樣數(shù)據(jù)   
                if (sampleBits === 8) {  
                    for (var i = 0; i < bytes.length; i++, offset++) {  
                        var s = Math.max(-1, Math.min(1, bytes[i]));  
                        var val = s < 0 ? s * 0x8000 : s * 0x7FFF;  
                        val = parseInt(255 / (65535 / (val + 32768)));  
                        data.setInt8(offset, val, true);  
                    }  
                } else {  
                    for (var i = 0; i < bytes.length; i++, offset += 2) {  
                        var s = Math.max(-1, Math.min(1, bytes[i]));  
                        data.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);  
                    }  
                }  
  
                return new Blob([data], { type: 'audio/wav' });  
            }  
        };  

通過點擊傳后臺

      $('#download').click(function(){

            var url = recorder.getBlob();
            console.log(url) //獲取到blob
            //Blob {size: 44, type: "audio/wav"}
            console.log(JSON.stringify(url))//這樣序列化 blob 直接變成空
              //{}
            
        //     var reader = new FileReader();
        //     reader.readAsText(url)
            
        // reader.onloadend = function(e) {
        //    var result = reader.result;
        //     $.post('yuyin',{voiceData:result},function(data){
        //         console.log(data)
        //     })
        //     console.log(result)
        // }
            // 上面這個方式過長
    $.post('yuyin',{voiceData:JSON.stringify(url)},function(data){
                console.log(data)
            })
       
     


     
    })
回答
編輯回答
哎呦喂
var fd = new FormData();
fd.append('fname', 'test.wav');
fd.append('data', soundBlob);
$.ajax({
    type: 'POST',
    url: '/yuyin',
    data: fd,
    processData: false,
    contentType: false
}).done(function(data) {
      console.log(data);
});
2017年3月14日 19:44