【问题标题】:How to send audio from ios and store in mysql using php?如何使用 php 从 ios 发送音频并存储在 mysql 中?
【发布时间】:2013-07-31 11:35:19
【问题描述】:

我必须从 iOS 应用程序发送音频文件并使用 php 存储在 MySQL 中。我已将音频文件转换为 base64string 并发送到服务器。问题是我只能在数据库中存储几个字节的数据。

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());
}

以上是iOS和php的代码。

【问题讨论】:

  • 如果不需要将文件存储在数据库中,我建议您考虑将文件存储在文件系统中,这是一种更好的方法,因为它是二进制文件。

标签: php ios


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请使用 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];
    

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 2012-08-27
      • 2017-12-29
      • 2018-06-07
      • 2012-07-07
      • 2022-01-01
      • 1970-01-01
      • 2019-08-27
      • 2017-06-20
      相关资源
      最近更新 更多