0

I am coding an application that needs to send audio files, in mp3 or any other common audio format, to a server. I am using a NSURLConnection instance to make this connection. I'm using the post method because the file that receives the information is a .php. In the same connection I need to send other parameters that are mostly strings and ints. The problem that I am facing is actually sending the audio file and receiving it in the server. I'm using the next code to build my connection:

NSMutableURLRequest *localRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"whateverphpfile"]];
[localRequest setHTTPMethod:@"POST"];
[self setRequest:localRequest];
[localRequest release];

NSMutableData *body = [[NSMutableData alloc] init];

NSString *testerSTR = [NSString stringWithFormat:@"file=%@", [[NSBundle mainBundle] pathForResource:@"header" ofType:@"png"]];
[body setData:[testerSTR dataUsingEncoding:NSUTF8StringEncoding]];

//_request is of type NSMutableURLRequest
[_request setHTTPBody:body];
//[_request setValue:@"multipart/form-data" forHTTPHeaderField:@"enctype"];
[_request setValue:@"multipart/form-data" forHTTPHeaderField:@"content-type"];

NSLog(@"the body sent is: %@", [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]);

_connection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];

if (_connection)
{
    [self setWebData:[NSMutableData data]];
}
else
{
    NSLog(@"theConnection is NULL");
}

and the problem when checking the data and returning it to the device is that it is a laaarge string like <564024S D451475 DS5752... ...552D4S5>. What should I do to receive the actual audio file or how can I transform that large string into an audio file readable by the native iOS classes?

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
Jonathan Ginsburg
  • 1,008
  • 1
  • 8
  • 15

1 Answers1

0

You need to send your data in multipart/form-data format. The second answer for this topic has code that should do just that: NSURLConnection to upload file asynchonrously?

You could also use the ASIHTTPRequest library which can do just that.

Community
  • 1
  • 1
Taum
  • 2,511
  • 18
  • 18