zoukankan      html  css  js  c++  java
  • '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没有值,也是醉了。。

     

     

  • 相关阅读:
    【WF2017】Mission Improbable
    【Codeforces 837D】Round Subset
    【Codeforces 788C】The Great Mixing
    【JSOI2008】最大数
    2.1图像的数字化
    MATLAB生成随机数
    四六级准考证号忘记了如何快速查询四六级成绩?
    加密与水印结合
    如何在 PyPI安装python的软件包?
    matlab中如何定义函数
  • 原文地址:https://www.cnblogs.com/symen/p/4950112.html
Copyright © 2011-2022 走看看