AFNetworking 3.0 上传图片
Posted
技术标签:
【中文标题】AFNetworking 3.0 上传图片【英文标题】:AFNetworking 3.0 upload image 【发布时间】:2016-07-13 11:58:24 【问题描述】:我正在尝试使用 AFNetworking 3.0 将图像上传到我的服务器。我的服务器返回“请选择要上传的文件。”。这是我如何在我的 php 文件 $filename = $_FILES["file"]["name"];
中捕获上传的文件。
-(void)uplodeImages :(NSString *)image
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://local/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileURL:[NSURL fileURLWithPath:image] name:@"file" fileName:@"imagename.jpg" mimeType:@"image/jpeg" error:nil];
error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress)
dispatch_async(dispatch_get_main_queue(), ^
NSLog(@"Laddar...");
);
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
if (error)
NSLog(@"Error: %@", error);
else
NSLog(@"%@ %@", response, responseObject);
];
[uploadTask resume];
【问题讨论】:
【参考方案1】:注意 :- 我刚刚使用 AFNetworking 3.0 实现了图片上传服务,
-> 这里 kBaseURL 表示服务器的 Base URL
->serviceName 表示服务名称。
->dictParams 表示需要时给出参数,否则为零。
->image 表示传递你的图像。
注意:- 这段代码是用 NSObject 类编写的,我们已经在我们的项目中应用了。
+ (void)requestPostUrlWithImage: (NSString *)serviceName parameters:(NSDictionary *)dictParams image:(UIImage *)image success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure
NSString *strService = [NSString stringWithFormat:@"%@%@",kBaseURL,serviceName];
[SVProgressHUD show];
NSData *fileData = image?UIImageJPEGRepresentation(image, 0.5):nil;
NSError *error;
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:strService parameters:dictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
if(fileData)
[formData appendPartWithFileData:fileData
name:@"image"
fileName:@"img.jpeg"
mimeType:@"multipart/form-data"];
error:&error];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress)
NSLog(@"Wrote %f", uploadProgress.fractionCompleted);
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
[SVProgressHUD dismiss];
if (error)
failure(error);
else
NSLog(@"POST Response : %@",responseObject);
success(responseObject);
];
[uploadTask resume];
--> 现在申请到我们的项目中。
UIImage *imgProfile = //Pass your image.
[WebService requestPostUrlWithImage:@"save-family-member" parameters:nil image:imgProfile success:^(NSDictionary *responce)
NSString *check = [NSString stringWithFormat:@"%@",[responce objectForKey:@"status"]];
if ([check isEqualToString: @"1"])
//Image Uploaded.
else
//Failed to Upload.
failure:^(NSError *error)
//Error
];
【讨论】:
以上是关于AFNetworking 3.0 上传图片的主要内容,如果未能解决你的问题,请参考以下文章