I am trying to upload the files created by the Audio Recorder
Here is the code that I am using:
var start = UI.createButton({title: "Record"});
start.addEventListener('touchstart', function(e) {
recorder.start();
start.title = "Recording...";
});
start.addEventListener('touchend', function(e) {
file = recorder.stop();
start.title = "Recorded: " + file.size;
var xhr = Titanium.Network.createHTTPClient();
xhr.onerror = function(e)
{
Ti.UI.createAlertDialog({title:'Error', message:e.error}).show();
Ti.API.info('IN ERROR ' + e.error);
};
xhr.setTimeout(20000);
xhr.onload = function(e)
{
Ti.UI.createAlertDialog({title:'Success', message:'status code ' + this.status}).show();
Ti.API.info('IN ONLOAD ' + this.status + ' readyState ' + this.readyState);
};
xhr.onsendstream = function(e)
{
Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
};
// open the client
xhr.open('POST','http://localhost:8097/v1/upload');
xhr.setRequestHeader("Content-Type", "audio/x-wav");
// send the data
xhr.send(file);
});
It records, saves a file in the tmp directory with the contents and even reports the correct file size. When I try to upload though, the content length is zero.
Sam
1 Answer
-
Using the source for Titanium Mobile, I figured out it is because the API for TiFile is wrong. I needed to do xhr.send(file.blob) to get the contents. Looks like it is documented as file.read to me.