• 'Invalid parameter not satisfying: body'


    afnetwork图片上传的时候出错,出现错误

    2015-11-09 15:47:59.086 videoPro[3207:132795] *** Assertion failure in -[AFStreamingMultipartFormData appendPartWithHeaders:body:], /Users/zhanglingxiang/Documents/videoPro/AFNetworking/AFURLRequestSerialization.m:793

    2015-11-09 15:48:01.655 videoPro[3207:132795] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: body'

     

    代码:

     NSData *imageData = UIImageJPEGRepresentation(image,0.1f);
        
        //转码成UTF-8  否则可能会出现错误
        NSString *URLTmp = [NSString stringWithFormat:@"%@/photoUpload",SERVER];
        
        NSString *URLTmps = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        URLTmp = URLTmps;
        
        NSDictionary* dic = [NSDictionary dictionaryWithObject:self.userName forKey:@"user"];
        
        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URLTmp parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            
            [formData appendPartWithFileData:imageData name:@"headerImg" fileName:@"123.png" mimeType:@"image/png"];
            
            
        } error:nil];
        
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        
        NSProgress *progress = nil;
    
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            
            if (error) {
                
                NSLog(@"Error: %@", error);
                
            } else {
                NSLog(@"%@",responseObject);
                NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                NSString* strHead = [NSString stringWithFormat:@"%@/%@",IMGSERVER,dic[@"url"]];
    //            [[NSUserDefaults standardUserDefaults] setObject: forKey:@"userHead"];
                [imageView sd_setImageWithURL:[NSURL URLWithString:strHead]];
                
                //头像上传成功
                MBProgressHUD* hud = [[MBProgressHUD alloc] initWithView:self.view];
                [self.view addSubview:hud];
                hud.mode = MBProgressHUDModeCustomView;
                hud.labelText = @"头像上传成功";
                [hud showAnimated:YES whileExecutingBlock:^{
                    sleep(2);
                } completionBlock:^{
                    [hud removeFromSuperview];
                    return;
                }];
    
                
            }
        }];
        [uploadTask resume];
    

      

     

    改了几种方法,都不行,最后发现是image没有值,也是醉了。。

     

     

  • 相关阅读:
    sh_02_判断年龄改进版
    面向对象-内置函数和内置方法
    python面向对象-三大特性
    面向对象编程
    面向对象编程-回合制游戏
    练习Dream-购物车
    bytes和str的区别与转换
    字符串的操作和使用方法。
    程序员必须掌握的600个单词
    移动端的网页设计流程有哪些?
  • 原文地址:https://www.cnblogs.com/symen/p/4950112.html
Copyright © 2020-2023  润新知