ios第三种post请求上传三张图片到服务器

//1.创建管理者对象

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"multipart/form-data",@"text/plain", nil];//如果报接受类型不一致请替换一致text/html或别的

    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    NSString *mytoken=[userDefaults stringForKey:@"mytoken"];

    

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

    [parameters setValue:mytoken forKey:@"token"];

    [parameters setValue:@"51" forKey:@"id"];

    [parameters setValue:imagedata forKey:@"file"];

    NSLog(@"字典的值:%@", parameters);

    //[portUrl stringByAppendingString:@"/rest/imgUpload/upload"]

    //@"0.tcp.ngrok.io:12436"

    //NSString *str1=@"0.tcp.ngrok.io:12436";

    [manager POST:[portUrl stringByAppendingString:@"/rest/imgUpload/upload"] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData_Nonnull formData) {

        

        //上传文件参数

        NSLog(@"我需要的相片data:%@", imagedata);

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        formatter.dateFormat = @"yyyyMMddHHmmss";

        NSString *str = [formatter stringFromDate:[NSDate date]];

        NSString *fileName = [NSString stringWithFormat:@"%@.png", str];

        NSLog(@"我需要的fileName:%@", fileName);

        [formData appendPartWithFileData:imagedata name:@"file" fileName:fileName mimeType:@"image/png"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {

        //打印上传进度

        CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;

        NSLog(@"%.2lf%%", progress);

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        

        dispatch_async(dispatch_get_main_queue(), ^{

            [tableFooterActivityIndicator stopAnimating];//菊花停止

            [tableFooterActivityIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏菊花

            self.view.userInteractionEnabled=YES;

            [darkview removeFromSuperview];

        });

        

        //请求成功

        NSLog(@"请求成功:%@",responseObject);

        NSLog(@"返回的说明desc:%@", [responseObject objectForKey:@"desc"]);

        NSString *theimagepath=[NSString stringWithFormat:@"%@", [responseObject objectForKey:@"desc"]];

        

        if (self.sheet.tag==2550) {

            self.imagefullpath1=[NSString stringWithFormat:@"%@", theimagepath];

            self.nameLabel2.text=@"上传成功";

            NSLog(@"第一张图片上传成功");

        } else if (self.sheet.tag==2551) {

            self.imagefullpath2=[NSString stringWithFormat:@"%@", theimagepath];

            self.sexLabel2.text=@"上传成功";

            NSLog(@"第二张图片上传成功");

        } else if (self.sheet.tag==2552) {

            self.imagefullpath3=[NSString stringWithFormat:@"%@", theimagepath];

            self.phoneLabel2.text=@"上传成功";

            NSLog(@"第三张图片上传成功");

        }

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        //请求失败

        NSLog(@"请求失败:%@",error);

        

        dispatch_async(dispatch_get_main_queue(), ^{

            [tableFooterActivityIndicator stopAnimating];//菊花停止

            [tableFooterActivityIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏菊花

            self.view.userInteractionEnabled=YES;

            

        });

        

        if (self.sheet.tag==2550) {

            self.nameLabel2.text=@"上传失败";

            NSLog(@"第一张图片上传失败");

        } else if (self.sheet.tag==2551) {

            self.sexLabel2.text=@"上传失败";

            NSLog(@"第二张图片上传失败");

        } else if (self.sheet.tag==2552) {

            self.phoneLabel2.text=@"上传失败";

            NSLog(@"第三张图片上传失败");

        }

    }];

}

猜你喜欢

转载自blog.csdn.net/mr_tangit/article/details/80613309
今日推荐