无法在iOS应用程序中使用AFNetworking

无法在iOS应用程序中使用AFNetworking

问题是服务器返回了json对象,但说“请选择一个文件”,即使我通过调用函数 appendPartWithFormData 将数据作为NSData传递。

请在这里帮助我,我在这方面停留了很久。

谢谢

// Defining dictionary parameters
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:regId forKey:@"id"];
[_params setObject:email forKey:@"email"];
[_params setObject:password forKey:@"password"];
[_params setObject:_formatedDate forKey:@"datesel"];
[_params setObject:self.documentDescription.text forKey:@"description"];
[_params setObject:self.documentName.text forKey:@"name"];
[_params setObject:self.documentType.text forKey:@"type"];
[_params setObject:@"no" forKey:@"isDepedent"];

NSData *imageData = UIImageJPEGRepresentation(self.finalImage.image, 1.0);


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager POST:url parameters:_params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
    [formData appendPartWithFormData:imageData name:@"image"];
}success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success %@",responseObject);

    UIAlertView *Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [Alert_Success_fail show];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Fail ");

    UIAlertView *Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [Alert_Success_fail show];
}];



OUTPUT - Success {
    data =     {
        message = "Please select file.";
       status = 0;
    };
 }

最佳答案

您可以将图像另存为文件,然后上传文件。
AFNetWorkSample:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

如果不起作用,则服务器可能有问题。

关于ios - 无法在iOS应用程序中使用AFNetworking,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30365558/

10-08 23:40