如何使用 AFNetworking 直接从 iPad 应用上传图片
How to Upload Images Using AFNetworking directly from iPad app
我对 iOS.I 完全陌生,想使用 AFNetworking 将图像上传到我的服务器。
我试过了,但还是没有完成。请帮助我,提前致谢
试试这个对我有用
-(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];
}
我对 iOS.I 完全陌生,想使用 AFNetworking 将图像上传到我的服务器。 我试过了,但还是没有完成。请帮助我,提前致谢
试试这个对我有用
-(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];
}