如何使用afnetworking上传base64中图像的多部分数据 [英] How to Upload Multipart data of image in base64 Using afnetworking

查看:289
本文介绍了如何使用afnetworking上传base64中图像的多部分数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下代码,但我得到的响应是 java.lang.NullPointerException & INTERNAL_SERVER_ERROR 我尝试了很多不同的方法,但无法修复它,请帮助解决这个问题。

I have used the following code but the response which i get is java.lang.NullPointerException & INTERNAL_SERVER_ERROR I tried many different methods but unable to fix it please help in fixing this.

获取来自图像选择器的图像

     UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
     Profilebackground.image = chosenImage;
    [picker dismissViewControllerAnimated:YES completion:NULL];
    NSURL *resourceURL;
    UIImage *image =[[UIImage alloc] init];

    image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

    imageName = [imagePath lastPathComponent];

    resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];



    NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:@"."].location+1 ];

    if ([extensionOFImage isEqualToString:@"JPG"])
    {
        imageData =UIImageJPEGRepresentation(image, 1.0);
        base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        extension=@"image/jpeg";

    }

    else
    {
        imageData =  UIImagePNGRepresentation(image);
        base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        extension=@"image/png";
    }

    int imageSize=imageData.length/1024;

    NSLog(@"imageSize--->%d", imageSize);
    if (imageName!=nil) {
        NSLog(@"imageName--->%@",imageName);
    }
    else
    {
        NSLog(@"no image name found");
    }

将图像发送到服务器

  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        [manager POST:@"https://blahblahblah.com/uploadProfileImg?userId=1" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            //NSData *pngData = [[NSData alloc] initWithBase64EncodedString:base64 options:1];
            [formData appendPartWithFileData:imageData
                                        name:@"key"
                                    fileName:imageName mimeType:extension];

        }  success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"Response: %@", responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
            NSLog(@"error: %@",error);
        // NSHTTPURLResponse *response = (NSHTTPURLResponse *)operation.response;
            NSLog(@"statusCode: %ld", (long)response.statusCode);
            NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
           NSLog(@"Error Response:%@",ErrorResponse);
        }];


推荐答案

你可以使用 appendPartWithFileData:name:fileName:mimeType: AFMultipartFormData 类的方法。

You can just use the appendPartWithFileData:name:fileName:mimeType: method of the AFMultipartFormData class.

For实例:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager POST:@"https://blahblahblah.com/imageupload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"key name for the image"
                            fileName:photoName mimeType:@"image/jpeg"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这篇关于如何使用afnetworking上传base64中图像的多部分数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆