I have controller method and javascript method for recording audio.
Controller:
This controller will return string data
@RequestMapping(value = "/sendAudioBlobData/{questionId}", headers = "content-type=multipart/*", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public String getBlob(HttpServletRequest request,HttpServletResponse response, ModelMap model, @RequestParam(value = "file") MultipartFile file, @PathVariable(value = "questionId") int questionId) {
boolean flag = false;
String id = Integer.toString(questionId);
String fileName = null;
byte[] bytes;
try {
bytes = file.getBytes();
if (!file.isEmpty()) {
id = id + ".mp3";
fileName = VOPortalConstants.NLE_USER_AUDIO_FILES_ROOT + File.separator + id;
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(fileName)));
stream.write(bytes);
stream.close();
File f = new File(fileName);
if(f.exists()) {
response.addHeader("Content-Length", f.length() + "");
FileUtils.sendFile(f, response.getOutputStream());
}
}
}
catch (IOException e) {
logger.error("ERROR: ", e);
}
String fName = fileName.replace(VOPortalConstants.NLE_PATH, VOPortalConstants.NLE_URL_CONST);
return fName;
}
JS Method:
This code provide that to request(POST) for recording user's audio
function saveAudioFile() {
var questionId = window.questionList[window.counter].id;
recorder && recorder.exportWAV(function(blob) {
var formData = new FormData();
formData.append("file", blob);
$.ajax({
url : "sendAudioBlobData/" + questionId,
type: 'POST',
data: formData,
dataType: "text",
contentType: false,
processData: false,
success: function(data) {
debugger
var audio = $("#your_voice");
$("#your_voice_source").attr("src", data);
/****************/
audio[0].pause();
audio[0].load();//suspends and restores all audio element
//audio[0].play();
/****************/
},
error: function() {
alert("not so boa!");
}
});
});
}
in controller, return value is string that looks as follows -> fName=/NLERESOURCES/NLE/VOICE/USERVOICE107.mp3
but return data that looks as follows in js -> RIFF$@WAVEfmt��������������������������������������������������������������������������������������������������������������������������������
how i do handle true string data?
Thanks
FileUtils.sendFile is returning the file as part of the Response outputstream from the controller.