有时候,项目中需要向服务器不单单是上传图片或者数据信息,要想将图片和一些数据信息一起上传到服务器,用AFNetWorking就可以实现:
//向服务器上传数据
-(void)importDate:(UIButton *)button {
if (_footerView.reason.text.length != 0) {
[self showHudInView:self.view hint:nil];
//要发送给服务器的数据类信息拼接在字典里即可
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_orderId ,@"orderId",_productId,@"productId",_footerView.amountLabel.text,@"count",_footerView.reason.text,@"remark", nil];
NSLog( @"申请退货的参数 %@",dic);
AFHTTPRequestOperationManager* mgr = [AFHTTPRequestOperationManager manager];
//发送请求 上传图片给服务器
[mgr POST:E_returnGoodsDetailURL parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> totalformData) {
//_imageArr是图片数组(一般是已经转化为NSData类型的图片的数组
一般方法为: NSData *imageData = UIImageJPEGRepresentation(_footerView.smallImage.image, 0.5);
)
for (int i = 0; i<_imageArr.count; i++) {
[totalformData appendPartWithFileData:[_imageArr objectAtIndex:i] name:[NSString stringWithFormat:@"image%i",i] fileName:[NSString stringWithFormat:@"image%i.jpg",i] mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self hideHud];
//成功回调
NSLog(@"成功回调====%@",responseObject);
if ([[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"success"]] isEqualToString:@"1"]) {
[self weiXinPayShowInfoViewWith:@"提交成功" ];
[self.navigationController popViewControllerAnimated:YES];
}else
{
[self weiXinPayShowInfoViewWith:[responseObject objectForKey:@"msg"] ];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败回调
[self hideHud];//根据情况来改变代码
} ];
}else
{
//写入需要的代码
}
}