2012-11-02 142 views
0

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

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地址:(空)”。我的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; 
} 

我真的很感激一点帮助。

回答

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 

不知道,如果是没有错误的,因为它已经从一个拍摄更长的来源,所以我修改了一些代码段

+0

thanx的代码,只有一两件事,当我使用HTML浏览测试我的PHP代码,我有这样的: 和我的图像名称是1.png。我有一个使用这些设置itemData和字典的问题。你能帮我吗? – AliBZ

+0

你只需要在NSData中转换你的UIImage。 with self.itemData = UIImagePNGRepresentation(yourUIImageInstanc E); 。在我的代码中,self.itemData是一个Property类型的NSData。 – LombaX

+0

你能再解释一次吗?我现在不明白你的问题。如果你想用浏览器检查你的php代码,你必须用一个表单和一个输入类型文件(你已经发布的行)制作一个简单的页面。表单方法是post和action是php接收者页面的地址。

然后只需选择文件并上传 – LombaX

0

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

SO Post Here

+0

我在某处看到SBJason已被删除。是对的吗? – AliBZ