Does any one have a full implementation of uploading a file using AFNetworking. I have found some code on the internet but it is incomplete. The code I have found is here:
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://my.client.server.com"]];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:[fieldName text] forKey:@"field01_nome"];
[parameters setObject:[fieldSurname text] forKey:@"field02_cognome"];
NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/Contents/mail/sendToForm.jsp" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"];
}];
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) {
NSLog(@"Success");
} failure:^(NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Fail");
}];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
I get errors on the following lines:
[formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"];
At the myNSDataToSend.
And here:
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) {
NSLog(@"Success");
} failure:^(NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Fail");
}];
The error is:
Class method '+HTTPRequestOperationWithRequest:success:failure' not found(return type defaults to 'id')
Any help on this and uploading with AFNetworks would be amazing.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…