I have a simple rails application in which I use HTML5 audio web api with recorder.js to record voice and then save it on the application server. The recording is normal, I can play the recording and hear the sound, but when I post it on the server, my audio file is empty.
html / js code
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."); } }; }
Controller code
def save_file audio = params[:audio] save_path = Rails.root.join("public/#{audio.original_filename}")
Console log
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>>}
source share