使用AFNetwork上传图像

时间:2012-11-02 21:13:16

标签: php objective-c upload afnetworking

我已经搜索了很多使用AFNetwork工作的上传代码,但没有运气。我想出了这个代码,我从2个不同的网站得到了它:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]);
NSURL *url = [NSURL URLWithString:@"http://localhost/project"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"google" fileName:@"1.png" mimeType:@"image/png"];
}];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
} failure:nil];

[operation start];

但我得到的是“IP地址:( null)”。我的php方面是这样的:

function upload(){
$uploaddir = 'uploads/';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    sendResponse(200, 'Upload Successful');
    return true;
}
sendResponse(403, 'Upload Failed');
    return false;
}

我非常感谢你的帮助。

2 个答案:

答案 0 :(得分:3)

我几天前就做过这个。 iPhone端的代码(它上传图像和另一个文本参数,项目)

NSString *uploadURL = @"http://www.baseurl.it/";
NSURL *url = [[NSURL alloc]initWithString:uploadURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *dict = [NSDictionary
                      dictionaryWithObjects:@[self.itemValue]
                      forKeys:@[@"item"]];
// self.itemValue is a post parameter type nsstring

NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"path/to/page.php" parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
// self.itemData is an NSData containing the image
    if (self.itemData)
        [formData appendPartWithFileData:self.itemData name:@"imageData" fileName:@"imageData.jpg" mimeType:@"image/png"];
}];

// sorry for the formatting here, is a long method using blocks
AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                   NSLog(@"Upload Success");
                                                                               }                                                                                   
                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                   NSLog(@"Error: %@", error.description);

                                                                               }];
[json start];

在Php方面:

// file upload
try {
$fileName = $_FILES['imageData']['name'];
$tmpName  = $_FILES['imageData']['tmp_name'];
$fileSize = $_FILES['imageData']['size'];
$fileType = $_FILES['imageData']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

} catch (Exception $e) {
    echo json_encode("Error:".$e);
}

// $content contains your image

不确定它是否没有错误,因为它是从较长的来源获取的,所以我修改了一些代码

答案 1 :(得分:0)

查看我对类似问题的回答。我使用这个自定义类(由我编写)(用于我的所有远程Web服务操作。它足够灵活,允许其他POST参数与图像文件数据一起发送.processFileUploadRequestWithURL方法是您需要的方法,其他方法你可以使用与否。

SO Post Here

相关问题