2011-01-12 55 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。

希望这有助于的