【发布时间】:2016-08-31 09:15:09
【问题描述】:
我正在尝试使用 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];
它将进入成功块并出现 error : error In Image Uploading。
我也尝试从 Postman 检查我的 API 响应,但从 Postman 可以正常工作。这段代码有什么问题?
【问题讨论】:
-
您能否添加一个邮递员截图,API 接受文件为 base64 或简单文件
-
对不起,我不能。但我可以告诉你所有的描述。我必须在邮递员中发送的图像是“文件”类型。并使用其他参数,例如名称,地址..等。我得到的响应是{“成功”:“交付注册成功”}
标签: ios objective-c afnetworking-3