2011-06-01 83 views
2

我正嘗試將「caf」格式的音頻文件從iPhone上傳到Web服務器。使用的代碼如下所示。問題是,我沒有得到任何文件上傳,沒有輸出文件名稱在PHP回聲!任何幫助將不勝感激。如何從iPhone上傳使用HTTP POST的音頻文件?

我使用在iPhone端的代碼是:

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"; 

感謝。

- 阿赫桑

編輯#1:

使用@Gypsa的編輯,我得到一個錯誤。代碼和錯誤消息如下:

代碼:

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的音頻文件的地址我做了:

NSString urlString=[url absoluteString]; 

但是,現在我沒有得到任何resp ONSE從我的PHP page..the響應爲空:(

編輯#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:

解決!看到答案:)

+0

什麼是響應狀態碼 – Gypsa 2011-06-01 11:24:47

+0

@Gypsa:響應狀態碼是0! – Ahsan 2011-06-01 11:38:01

+0

現在就解決您的問題 – Gypsa 2011-06-01 12:43:34

回答

3

我已經使用此代碼進行視頻上傳,您可以使用它進行音頻上傳。

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]); 
+0

@Gypsa:一旦添加視頻文件就會出錯。請看更新後的帖子! – Ahsan 2011-06-01 11:09:00

+0

你得到什麼錯誤 – Gypsa 2011-06-01 11:11:50

+1

你有沒有一個路徑作爲你的音頻文件的nssring – Gypsa 2011-06-01 11:12:29

相關問題