0

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

我真的很感激一点帮助。

4

2 回答 2

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

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

于 2012-11-02T21:35:45.860 回答
0

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

所以在这里发帖

于 2012-11-02T21:43:24.490 回答