0

我想将图像上传到服务器上的文件夹。图像被发送到objective-c 中的一个方法到一个php 脚本。但是上传没有发生!有什么问题?

这里是 obj-C 方法:

NSURLConnection *getConnection;
NSData *imgData = UIImageJPEGRepresentation(self.imageView.image, 0.7);
NSMutableString *strURL = [NSMutableString stringWithFormat:@"http://.../uploadPhoto.php?ID=2&ph=%@",imgData];

[strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strURL]];
[request setHTTPMethod:@"GET"];
getConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

这里的php脚本:

$ID = $_GET[ID];
    $ph = $_FILES['ph'];
    $filename =$ID;
    move_uploaded_file($ph['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/users/$ID/$filename");

请帮我!

4

1 回答 1

0

您必须使用“POST”方法,而不是“GET”。

这是一个不错的 iOS 网络库: https ://github.com/AFNetworking/AFNetworking

此页面上有一些代码示例,可以帮助您。

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"      path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id  <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg"  mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];

您可以在 GitHub 或此处阅读更多内容:http: //afnetworking.com/

于 2013-07-25T09:29:39.563 回答