我正在尝试将图像上传到服务器(已经构建)并且我收到诸如请求已超时之类的错误。从服务器发送文本和获取数据的其他方法工作正常。但是,发送图像我发现很难做到。
我目前正在使用以下代码:
-(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];
}
你知道问题是什么吗?你能给我一些关于上述代码的建议或可能的错误吗?
非常感谢。