Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked October 3rd 2010 by Sam Pullara
  • audio
  • file
  • recording
  • upload
  • xhr
0 Comments

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.

    — answered October 3rd 2010 by Sam Pullara
    permalink
    2 Comments
    • thank you for this! i needed that last comment very badly!

      — commented November 30th 2011 by Mendel Broekhuijsen
    • I'm trying to upload my audio but on my php it says that my $_FILES['file'] is empty

      can you help me? can you post your .php file to receive the audio ?

      Thanks

      — commented October 1st 2014 by Douglas Hennrich
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.