使用AFNetworking发布图像?

时间:2014-05-09 13:23:04

标签: ios post afnetworking image-uploading

如何使用AFNetworking发布图片。我使用下面的代码发布图片。但是图片没有上传到服务器。发布图片后我得到了结果中的这个链接" {{3 }}"

有谁能告诉我代码中的问题是什么?

这是我的代码:

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:BaseUrl]];
     NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:posturl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFormData:imgData name:@"image"];

    }];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *dict  = (NSDictionary *)JSON;

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    }];
    [operation start];

2 个答案:

答案 0 :(得分:2)

来自Github read me

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];

答案 1 :(得分:1)

以下是我的表现(在AFNetworking 2.x中):

- (void)requestWithFormData:(void (^)(id <AFMultipartFormData> formData))formBlock
                 withParams:(NSDictionary *)params
             webservicePath:(NSString *)webservicePath
                    success:(void (^)(AFHTTPRequestOperation *operation, id jsonDictionary))successBlock
                    failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBlock
{
    // Handle request for file upload
    AFHTTPRequestOperation *op = [self.manager POST:webservicePath
                                         parameters:params
                          constructingBodyWithBlock:formBlock
                                            success:success
                                            failure:failureBlock];
    [op start];
}

然后我按照以下方式使用它:

void (^formBlock)(id <AFMultipartFormData> formData) = ^(id <AFMultipartFormData>formData)
{
    NSData *data1 = UIImagePNGRepresentation(profileImage);
    [formData appendPartWithFileData:data1 name:@"file" fileName:@"picture.png" mimeType:@"image/png"];
};

[networkingUtil requestWithFormData:formBlock
                         withParams:params
                     webservicePath:@"myserver.com/upload"
                            success:successBlock
                            failure:failureBlock];

您的服务器应处理表单数据并查找file