【发布时间】: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