【问题标题】:Uploading Audio File using HTTP POST in Objective-C在 Objective-C 中使用 HTTP POST 上传音频文件
【发布时间】:2011-01-12 22:56:29
【问题描述】:

嗨,

我一直在尝试通过实现以下代码,使用 HTTP POST 方法将音频文件 (sample.wav) 上传到我的服务器。连接到服务器没有错误,但文件没有上传。我花了几个小时寻找解决方案,但还没有找到。这是我用来完成任务的代码。

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

最后一件事我们必须传递输入字段的名称,在这种情况下是“文件名”。我不知道如何在objective-c中传递这个。所以请有人可以指出我正确的方向。任何形式的帮助将不胜感激。

问候,

阿尔斯兰

【问题讨论】:

标签: iphone objective-c ios4


【解决方案1】:

您的 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。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多