如何在iPhone应用程序中将音频文件上传到服务器?

问题描述:

NSString *urlString1 = @"http://www.php.eminosoft.com/123singles/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString1]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

 NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"song.mp3\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSString stringWithString:secondString]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"demo"] dataUsingEncoding:NSUTF8StringEncoding]]; 


[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"5"] dataUsingEncoding:NSUTF8StringEncoding]]; 


[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

我得到的答复是:

文件上传成功"

"file uploaded successfully"

但是问题是文件内容的大小以字节为单位,音频文件不支持播放.

but the problem is the file content size is in bytes, the audio file is not support to play.

请帮助我,如何将音频文件上传到服务器?

Please help to me, How to upload a audio file to server?

我正在使用ASIHTTPRequest上传如下所示的音频文件

I am using ASIHTTPRequest to upload an audio file like below,

NSURL *audiourl = [NSURL URLWithString:@"Your Url"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
NSData *postData = [NSData dataWithContentsOfURL:SoundPath];
//SoundPath is your audio url path of NSDocumentDirectory.
[request addData:postData withFileName:@"myAudio.caf" andContentType:@"audio/caf" forKey:@"company_audio"];
[request setDelegate:self];
[request startSynchronous];

通过这种方式,您可以轻松地将音频文件上传到服务器上.

This way you can easily upload audio file on server.