我正在尝试使用 AFNetworking 3.0 将图像上传到服务器。
这是我的代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[[manager POST:setUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
{
//Current date with image name
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
[formData appendPartWithFileData:image name:namePara fileName:[NSString stringWithFormat:@"img_%@",[dateFormatter stringFromDate:[NSDate date]]] mimeType:@"image/png"];
}progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject)
{
NSLog(@"%@",responseObject);
[GMDCircleLoader hideFromView:self.view animated:YES];
NSMutableDictionary *dir = [[NSMutableDictionary alloc]initWithDictionary:responseObject];
if([dir valueForKey:@"error"])
{
UIAlertController *alert = [[singletone sharedManager]showAlert:NSLocalizedString(@"SIGNIN", nil) :[NSString stringWithFormat: @"%@",[dir valueForKey:@"error"]] ];
[self.parentViewController presentViewController:alert animated:YES completion:nil];
}
else
{
//Successfull
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"REGISTRATION", nil) message:[dir valueForKey:@"success"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * action)
{
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
NSLog(@"response object : %@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"failure : %@",error.localizedDescription);
}]resume];
它将进入成功块错误:图像上传错误。
我也尝试从 Postman 那里检查我的 API 响应,但是从 Postman 那里工作得很好。这段代码有什么问题?
尝试下面的代码,它可以与 AFNetworking 3.0 一起使用:
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:BaseURL parameters:<parameter dictionary> constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if (profile_pic.length!=0) {
[formData appendPartWithFileData:imageData name:@"profile_pic" fileName:@"ProfilePic" mimeType:@"image/jpeg"];
}
} error:nil];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error)
{
}
else
{
}
}];
[uploadTask resume];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)