2011-01-12 54 views
0

HI使用HTTP POST,上傳音頻文件在Objective-C

我一直在試圖上傳的音頻文件(sample.wav)使用通過實施以下代碼HTTP POST方法我的服務器。連接到服務器沒有錯誤,但該文件未上傳。我花了幾個小時尋找解決方案,但還找不到。這是我一直用來完成任務的代碼。

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"wav"]; 

    // NSLog(@"filePath : %@", filePath); 



    NSData *postData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSLog(@"postLength : %@", postLength); 



    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

    [request setHTTPMethod:@"POST"]; 

    [request setURL:[NSURL URLWithString:@"http://exampleserver.com/upload.php"]]; 



    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 



    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 



    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    [request setHTTPBody:postData]; 

    [request setTimeoutInterval:30.0]; 



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



    if (conn) 

    { 

     receivedData = [[NSMutableData data] retain]; 

    } else { 

     NSLog(@"Connection Failed"); 

    } 



    //[request release]; 

然後,我使用NSURLConnection的委託方法從我的服務器獲取響應。它確實嘗試連接到服務器,但沒有響應。在這方面,任何人都可以幫助我。這裏還有我一直用來通過簡單的網頁瀏覽器上傳的簡單的HTML代碼。

<form action="http://exampleserver.com/upload.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data" > 



         <input type="file" name="filename" /> 

,也是我們必須要通過的是在這種情況下,「文件名」在這裏輸入字段的名稱最後一件事。我不知道如何在目標-c中通過這個。所以請有人指出我正確的方向。任何形式的幫助將不勝感激。

問候,

阿爾斯蘭

+0

可能重複[我怎樣才能將照片上傳到與iPhone的服務器?(http://stackoverflow.com/questions/125306/how-can-i-upload-a -Photo到一個服務器,與最iphone) – 2011-01-12 22:59:16

回答

3

你缺少你的HTTP編碼某些位。你不能只追加文件數據。

您的HTTP正文應包括:

NSMutableData *postData = [NSMutableData data]; 
NSString *header = [NSString stringWithFormat:@"--%@\r\n", boundary] 
[postData appendData:[header dataUsingEncoding:NSUTF8StringEncoding]]; 

//add your filename entry 
NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"filename", @"your file name"]; 

[postData appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]]; 

[postData appendData:[NSData dataWithContentsOfFile:@"your file path"]; 
NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary]; 

[postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:postData]; 

與該代碼,你會更接近你的HTTP服務器的期望。爲了完成您的工作,您可以考慮使用Wireshark來區分哪些字節通過代碼和字節進行傳輸。

HTTP編碼「很難自己解決」代碼。您可以放棄此代碼並使用更高級別的庫,例如GTMHTTFetcher或ASIHTTPRequest。

希望這有助於的