0

I have to send a audio file from and iOS app and store in MySQL using php. I have converted the audio file to base64string and sent to the server. The problem is I can store only few bytes of data in the database.

NSURL *aUrl = [NSURL URLWithString:@"URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

[request setHTTPMethod:@"POST"];
NSString *postString = [NSString  stringWithFormat:@"audiostring=%@,",audioStr];

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(!connection){
    NSLog(@"Connection Failed");
}
if($_POST["audiostring"])
{
    $sql="insert into AudioDetails (audiostring)values('$_POST[audiostring]')";
    $result=mysql_query($sql) or die(mysql_error());
}

Above are the code for iOS and php.

4

2 回答 2

0

请使用 post 方法制作 http 正文。并使用 ASIFormDataRequest 框架将数据发送到 php 服务器。

asirequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; 
[asirequest setRequestMethod:@"POST"];  
[asirequest setPostValue:appDelegate.loggedInUserInfo.userID forKey:@"iSenderID"];                   
[asirequest setPostValue:@"0" forKey:@"iReceiverID"];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.wav"];
NSData *imageData = [NSData dataWithContentsOfFile:soundFilePath];

[asirequest setData:[imageData copy] withFileName:@"Sound.wav" andContentType:@"Audio/mp3" forKey:@"vSoundName"];

[asirequest startAsynchronous];
于 2013-07-31T12:18:50.440 回答
0

不要将 base64 字符串存储在数据库中,只需将字符串解码并在文件系统中创建一个物理文件,然后将此文件的引用放入数据库中。

于 2013-07-31T11:57:42.597 回答