1

我正在尝试将图像上传到服务器(已经构建)并且我收到诸如请求已超时之类的错误。从服务器发送文本和获取数据的其他方法工作正常。但是,发送图像我发现很难做到。

我目前正在使用以下代码:

-(void)uploadImage:(NSData*)image callbackBlock: (void (^)(BOOL success)) callbackBlock
{
    NSString *path = [NSString stringWithFormat:@"upload"];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"image", nil];
    [params addEntriesFromDictionary:self.sessionManager.authParameters];



    NSMutableURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData){


        [formData appendPartWithFormData:image name:@"Image"];

    }];

    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"!!!Response object: %@",responseObject);


        callbackBlock(YES);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Failure: %@",error.description);

        callbackBlock(NO);
    }];

    [self enqueueHTTPRequestOperation:operation];
}

你知道问题是什么吗?你能给我一些关于上述代码的建议或可能的错误吗?

非常感谢。

4

1 回答 1

0

您可以将图像作为 base64 编码文本发送......这应该可以。您可以使用此类别创建 base64 编码图像:

https://github.com/l4u/NSData-Base64

于 2013-10-31T11:21:59.823 回答