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

时间:2015-05-21 05:52:00

标签: ios objective-c xcode 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;
    };
 }

1 个答案:

答案 0 :(得分: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);
}];

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