首页 > 代码库 > iOS 提交form表单,上传图片
iOS 提交form表单,上传图片
之前不会用,总感觉很难,用后感觉不是太难,本文只是简单的讲一下怎么使用的,
//实例话对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer.timeoutInterval = 20; manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]; //在请求头里 添加自己需要的参数 NSString *token = [[YXLoginStatusTool sharedLoginStatus] getTokenId]; [manager.requestSerializer setValue:token forHTTPHeaderField:@"id"]; //请求地址 NSString *URLSTR = @""; [manager POST:URLSTR parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//转成data
self.justimagedata = http://www.mamicode.com/UIImagePNGRepresentation(pickerImagePic);
//按照表单格式把二进制文件写入formData表单 [formData appendPartWithFileData:self.justimagedata name:@"" fileName:@"" mimeType:@"image/png"]; [formData appendPartWithFileData:self.backimagedata name:@"" fileName:@"" mimeType:@"image/png"]; } progress:^(NSProgress * _Nonnull uploadProgress) { YXLog(@"++++%@+++",uploadProgress); } success:^(NSURLSessionDataTask *task, id responseObject) { YXLog(@"responseObject == %@--++--%@----", [self getResponseObjcWithTask:responseObject],[self getRespodHeaderWithTask:task]); if ([[self getRespodHeaderWithTask:task][@"Status"] isEqualToString:@"1"]) { //成功 }else{ [YXAlearMnager ShowAlearViewWith:[self getRespodHeaderWithTask:responseObject][@"errorMsg"] Type:2]; } [YXNetworkHUD dismiss]; } failure:^(NSURLSessionDataTask *task, NSError *error) { [YXNetworkHUD dismiss]; YXLog(@"发送失败+++++%@++++%@",error,[self getRespodHeaderWithTask:task]); [YXAlearMnager ShowAlearViewWith:@"头像更换失败" Type:2]; }];
//** 获取响应头 */ - (id)getRespodHeaderWithTask:(NSURLSessionTask *)task { NSHTTPURLResponse *respond = (NSHTTPURLResponse *)task.response; return respond.allHeaderFields; } -(id)getResponseObjcWithTask:(id )responseObect{ id json; NSError *error; json = [NSJSONSerialization JSONObjectWithData:responseObect options:0 error:&error]; if (error) { json = [[NSString alloc] initWithData:responseObect encoding:NSUTF8StringEncoding]; } return json; }
iOS 提交form表单,上传图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。