我有一个简单的 Rails 应用程序,我在其中使用 HTML5 音频 Web api 和 recorder.js 来录制语音,然后将其保存在应用程序服务器上。录音进行得很好,我可以重播录音并听到声音,但是当我将它发布到服务器上时,我的音频文件是空白的。
html/js 代码
function stopRecording() {
recorder.stop();
recorder.exportWAV(function(s) {
audio.src = window.URL.createObjectURL(s);
sendWaveToPost(s);
});
}
function sendWaveToPost1(blob) {
alert('in');
var data = new FormData();
data.append("audio", blob, (new Date()).getTime() + ".wav");
var oReq = new XMLHttpRequest();
oReq.open("POST", "/audio/save_file");
oReq.send(data);
oReq.onload = function(oEvent) {
if (oReq.status == 200) {
console.log("Uploaded");
} else {
console.log("Error " + oReq.status + " occurred uploading your file.");
}
};
}
控制器代码
def save_file
audio = params[:audio]
save_path = Rails.root.join("public/#{audio.original_filename}")
# Open and write the file to file system.
File.open(save_path, 'wb') do |f|
f.write params[:audio].read
end
render :text=> 'hi'
end
控制台日志
Parameters: {"audio"=>#<ActionDispatch::Http::UploadedFile:0xb61fea30
@original_filename="1379157692066.wav", @content_type="audio/wav", @headers="Content-
Disposition: form-data; name=\"audio\"; filename=\"1379157692066.wav\"\r\nContent-Type:
audio/wav\r\n", @tempfile=#<File:/tmp/RackMultipart20130914-3587-tgwevx>>}