我是iOS新手。我想使用 AFNetworking 将图片上传到我的服务器。 我试了但是还没完成。请帮助我,提前谢谢
答案 0 :(得分:4)
尝试使用它为我工作
-(void) uplodeImages{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"url where to upload images"]];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"cat1.png"]);
NSDictionary *parameters = @{@"username": @"", @"password" : @""};
AFHTTPRequestOperation *op = [manager POST:@"cat1.png" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:@"userfile" fileName:@"cat1.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
}