我正在发送带有参数但无法正常工作的邮件,在Postman Itis中工作,请参见下面的屏幕截图ios - 如何在AfNetworking中发送带有参数的图像-LMLPHP

NSDictionary * Param = @ {@“ api_token”:@“ 6fkgRh72y6L8DJi1zgYJr55zA0l3vrgnUOU3w6qFDCgX6e0QzwPLwT5D8nOHs8Ye35kFCjrAzSDNYvSsvkxJrmnYXYZYZYZYZYZYZYZXZYZNJJJJJJJJN

                                             };
                        NSData *imageData = UIImagePNGRepresentation(image);
                        NSString*url=[NSString stringWithFormat:@"http://asinfrastructure.com/mazad/public/api/v1/user/auction/media/upload/photo"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
                        [manager POST:url parameters:Param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                            [formData appendPartWithFileData:imageData
                                                        name:@"photo"
                                                    fileName:@"Image.png" mimeType:@"image/jpeg"];


                        } progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                            NSLog(@"Response: %@", responseObject);
                        } failure:^(NSURLSessionDataTask *task, NSError *error) {
                            NSLog(@"Error: %@", error);
                            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] options:NSJSONReadingAllowFragments error:&error];

                            NSLog(@"%@",JSON);
                        }];

最佳答案

您的代码运行良好。


-(void)uploadImage1:(UIImage *)img Dictionary:(NSMutableDictionary *)dictParam {
        NSData *imageData = UIImageJPEGRepresentation(img, 0.7);

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager POST:kWOJSignUPCall parameters:dictParam constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            if (imageData != nil) {
                [formData appendPartWithFileData:imageData
                                            name:@"profile_image"
                                        fileName:@"user_image.jpg"
                                        mimeType:@"image/jpg"];
            }


        } progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"Response: %@", responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"Error: %@", error);
            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] options:NSJSONReadingAllowFragments error:&error];

            NSLog(@"%@",JSON);
        }];
    }


或尝试一下。

-(void)uploadImage1:(UIImage *)img Dictionary:(NSMutableDictionary *)dictParam {
    NSData *imageData;
    if (img == nil) {

    }
    else {
        imageData = UIImageJPEGRepresentation(img, 0.7);
    }

    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];

    requestSerializer = [AFJSONRequestSerializer serializer];
    responseSerializer = [AFJSONResponseSerializer serializer];

    NSError *__autoreleasing* error = NULL;

    NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString:kWOJSignUPCall parameters:dictParam constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (imageData != nil) {
            [formData appendPartWithFileData:imageData
                                        name:@"profile_image"
                                    fileName:@"user_image.jpg"
                                    mimeType:@"image/jpg"];
        }
    } error:(NSError *__autoreleasing *)error];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    manager.responseSerializer = responseSerializer;

    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {

    }
                                      completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                          if (error) {
                                              NSLog(@"ERROR WHILE UPLOADING IMAGE = %@",error.localizedDescription);
                                          }
                                          else {
                                              if ([[responseObject valueForKey:@"status"] intValue] == 1) {
                                                  NSLog(@"Image Upload Done.");
                                              }
                                              else {
                                                  NSLog(@"Image Upload Fails.");
                                              }
                                          }
                                      }];

    [uploadTask resume];
}

10-08 17:48