问题描述
我正在尝试将caf格式的音频文件从iPhone上传到网络服务器。使用的代码如下。问题是,我没有上传任何文件,PHP echo中的文件名没有输出!任何帮助将不胜感激。
I am trying to upload an audio file in "caf" format from the iPhone to a web server. The used codes are given below. The problem is, I am not getting any file to upload, there is not output for the file names in PHP echo ! Any help would be greatly appreciated.
我在iPhone端使用的代码是:
The code I am using at the iPhone end is:
NSData *fileData=[NSData dataWithContentsOfURL:recordedTmpFile];
NSURL *url=[NSURL URLWithString:@"http://somelink.com/upload.php"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"audio/x-caf; boundary=%@",boundary];
[request setValue: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=\"userfile\"; filename=\"%@\"\r\n",[recordedTmpFile lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: audio/x-caf\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:fileData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSError *errr=nil;
NSURLResponse *resp=nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&errr];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
PHP代码是:
echo "start";
echo "1".$_FILES['userfile']['name'];
echo "2".$_FILES['userfile']['type'];
echo "end";
谢谢。
-
ahsan
-ahsan
编辑#1:
使用@Gypsa的编辑,我收到错误。代码和错误消息如下:
using @Gypsa 's edit, I get an error. Code and Error message follows:
代码:
NSString *urlString = @"http://somelink/upload.php";
NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
NSLog(@"3333");
//NSString *
[request addFile:recordedTmpFile forKey:@"userfile"];
[request setRequestMethod:@"POST"];
//[request setDelegate:self];
NSLog(@"555");
[request setTimeOutSeconds:500];
[request startSynchronous];
NSLog(@"666");
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseString %@",[request responseString]);
错误消息:
2011-06-01 07:06:10.819 viewer1[16473:207] -[NSURL length]: unrecognized selector sent to instance 0x5644900
2011-06-01 07:06:10.821 viewer1[16473:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to instance 0x5644900'
terminate called after throwing an instance of 'NSException'
编辑#2:
看起来我需要将音频文件的地址从NSURL转换为NSString我做了:
Edit #2:Looks like I needed to make the address of the audio file from NSURL to NSString which I did :
NSString urlString=[url absoluteString];
但是,现在我没有从我的php页面得到任何回复..响应为空:(
However, now I am not getting any response from my php page..the response is null :(
编辑#3:
我收到的错误信息是:
responseError Error Domain=ASIHTTPRequestErrorDomain Code=6 "No file exists at file://localhost/var/folders/45/45y8AmvjHvyUUl0mCOpmC++++TI/-Tmp-/328620103851.caf" UserInfo=0x534e500 {NSLocalizedDescription=No file exists at file://localhost/var/folders/45/45y8AmvjHvyUUl0mCOpmC++++TI/-Tmp-/328620103851.caf}
回复状态代码为0.
编辑#4:
解决!!!看到答案:)
Solved !!! See the answer :)
推荐答案
我用过这个视频上传代码,您可以将其用于音频上传。
I have used this code for video upload, you can use it for your audio upload.
NSString *str = [NSString stringWithFormat:@"%@/uploadVideoIphone.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *filePath = [urlvideo path];
[request addFile:filePath forKey:@"uploadfile"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:500];
[request startSynchronous];
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseString %@",[request responseString]);
这篇关于如何使用来自iPhone的HTTP POST上传音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!