【问题标题】:Upload an image to a server folder (objective-c method to php script)将图像上传到服务器文件夹(objective-c 方法到 php 脚本)
【发布时间】:2013-07-25 09:14:40
【问题描述】:

我想将图像上传到我服务器上的文件夹。图像被发送到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");

请帮帮我!

【问题讨论】:

  • 尝试 POST 方法,[请求 setHTTPMethod:@"POST"],我不确定

标签: php objective-c upload uiimage


【解决方案1】:

你必须使用“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/

【讨论】:

  • 在 php 脚本中我应该改变什么?
猜你喜欢
  • 2013-12-01
  • 2010-12-28
  • 1970-01-01
  • 2019-02-20
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多