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中通过这个。所以请有人指出我正确的方向。任何形式的帮助将不胜感激。
问候,
阿尔斯兰
可能重复[我怎样才能将照片上传到与iPhone的服务器?(http://stackoverflow.com/questions/125306/how-can-i-upload-a -Photo到一个服务器,与最iphone) – 2011-01-12 22:59:16