// 1.请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSString *url = [NSString stringWithFormat:@"%@/App/Index/userphoto/",SERVERURL];
// 2.拼接请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"mobile"] = self.userMobileLbl.text;
// 3.发送请求
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 拼接文件数据
NSData *data = UIImageJPEGRepresentation(localImg, 1.0);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
[formData appendPartWithFileData:data name:@"img1" fileName:fileName mimeType:@"image/jpg"];
} success:^(AFHTTPRequestOperation *operation, NSDictionary *json) {
// LXLog(@"%@", json);
[MBProgressHUD hideHUD];
[json[@"status"] isEqual: @"1"] ? [MBProgressHUD showSuccess:@"上传成功!"]:[MBProgressHUD showError:@"上传失败!"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD showError:@"上传头像失败,请重新上传"];
}];
网友评论