我正在使用此代码将音频文件从 iphone 应用程序上传到服务器。
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:mediaPath error: NULL];
NSLog(@"%@",fileAttributes);
if (fileAttributes != nil) {
NSNumber *fileSize;
fileSize = [fileAttributes objectForKey:NSFileSize];
NSLog(@"%@",fileSize);
// NSData *audioData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:audioData ofType:@""]];
NSString *urlString = [NSString stringWithFormat:@"%@audioupload.php",mydomainurl];
NSLog(@"%@",audioData);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------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:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".caf\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:audioData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
这样做会将 .caf 文件转换为二进制文件,并且在服务器中上传音频需要花费太多(1-2 分钟)时间。不仅如此,服务器中的文件大小越来越大。我已经包括
ASIFormDataRequest
但不知道如何在我的代码中使用它。谁能建议我如何减少上传时间。
但