嗯 之前在网上找了很多资源,看了很多,也很乱,大概总结了一下
1)上传一张图片:(该段代码是自己项目中的一段代码,为注册模块)
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];;
NSDictionary *dic=@{@”tel”:self.tel,@”pwd”:self.pwd,@”shop_name”:self.shopName.text,@”address”:self.adress,@”lat”:self.lat,@”lng”:self.Ing};
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@”Documents”] stringByAppendingPathComponent:@”currentImage.png”];
NSURL *url=[NSURL fileURLWithPath:fullPath];
[manager POST:@”http://appservice.ubi.cn/cheyouhui/merchant.php?m=Register&a=register” parameters:dic constructingBodyWithBlock:^(id formData)
{
[formData appendPartWithFileURL:url name:@”currentImage” error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@”Success: %@”, responseObject);
_arrRegister = [NSMutableArray arrayWithObjects:@"", nil];
EDTabBarController *root = [[EDTabBarController alloc]init];
[UIApplication sharedApplication].keyWindow.rootViewController = root;
//弹出提示框
[EDPoping showInRect:CGRectMake(80, 120, self.view.frame.size.width - 160, 100) text:@"登录成功" image:[UIImage imageNamed:@"newSuccess"]];
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
//时间测试
dispatch_after(when, dispatch_get_main_queue(), ^{
[EDPoping dismiss];
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
2)上传多张图片
//上传多张图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSString *merchant_shop_id=[defaults objectForKey:@"merchant_shop_id"];
NSDictionary *dic=@{@"merchant_shop_id":merchant_shop_id};//参数
[manager POST:@"http://appservice.ubi.cn/cheyouhui/merchant.php?m=User&a=upload_shop_img" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if(_arr.count>0)
{
NSObject *firstObject=[_arr objectAtIndex:0];
if([firstObject isKindOfClass:[UIImage class]])
{
for(NSInteger i=0;i<_arr.count;i++)
{
UIImage *eachImage=[_arr objectAtIndex:i];
if(eachImage!=nil)
{
// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSData *eachImageData=UIImageJPEGRepresentation(eachImage, 0.5);
NSDateFormatter *formatterr=[[NSDateFormatter alloc]init];
// 设置时间格式
formatterr.dateFormat=@"yyyyMMddHHmmss";
NSString *str=[formatterr stringFromDate:[NSDate date]];
NSString *fileName=[NSString stringWithFormat:@"%@.jpg",str];
/*
Data: 需要上传的数据
name: 服务器参数的名称
fileName: 文件名称
mimeType: 文件的类型
*/
[formData appendPartWithFileData:eachImageData name:[NSString stringWithFormat:@"file%d",i+1] fileName:fileName mimeType:@"image/jpeg"];
}
}
}
}
}success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//请求成功
NSLog(@"请求成功 %@",responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
//请求失败
NSLog(@"请求失败 %@",error);
}];
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
//时间测试
dispatch_after(when, dispatch_get_main_queue(), ^{
[MBProgressHUD showSuccess:@"上传成功"];
});
[self.navigationController popViewControllerAnimated:YES];
}
又如
3)
- (IBAction)upLoad
{
// 创建一个管理者
AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
// 设置参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@”username”] = @”123”;
params[@”pwd”] = @”123”;
// 设置URL
NSString *url = @"http://192.168.15.56:8080/MJServer/upload";
[manger POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
注意:
在发送请求之前会自动调用这个block
需要在这个block中添加文件参数到formData中
NSData *fileData = UIImageJPEGRepresentation(self.imageV.image, 1.0);
[formData appendPartWithFileData:fileData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
}
注意:
1.保留照片的质量
NSData *fileData = UIImageJPEGRepresentation(self.imageV.image, 1.0);
2.AFN不能监听文件的上传进度,ASI可以监听文件的上传进度。
3.需要上传的文件的具体数据
FileData : 需要上传的文件的具体数据
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
NSData *fileData = UIImageJPEGRepresentation(self.imageV.image, 1.0);
[formData appendPartWithFileData:fileData name:@”file” fileName:@”image.png” mimeType:@”image/png”];
4.需要上传的文件的URL路径
FileURL : 需要上传的文件的URL路径
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
NSURL *url = [[NSBundle mainBundle] URLForResource:@”itcast” withExtension:@”txt”];
[formData appendPartWithFileURL:url name:@”file” fileName:@”test.txt” mimeType:@”text/plain” error:nil];