AFNetwork上传文件,下载文件,监控网络状态


//  ViewController.m

//  1122

//

//  Created by SSP_MACMINI-001 on 15/6/30.

//  Copyright (c) 2015 sunhr. All rights reserved.

//


#import "ViewController.h"

#import "AFNetworking.h"

#import "UIKit+AFNetworking.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


//上传文件(上传图片)

-(void)testUploadFile{

//POST上传的接口

NSString *urlString = @"http://quiet.local/uploadtest/pk.php";

//参数名:image : 参数值是图片

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFCompoundResponseSerializer serializer];

[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSString *path = [[NSBundle mainBundle]pathForResource:@"daji.jpg" ofType:nil];

[formData appendPartWithFileURL:[NSURL URLWithString:path] name:@"image" fileName:@"666.jpg" mimeType:@"image/jpeg" error:nil];

 } success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }];

[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

//实现:上传的数据附加到请求体中

//fileName:上传后的文件名

//mimeType:需要上网搜索mime,把相应的格式复制过来

NSString *path = [[NSBundle mainBundle]pathForResource:@"daji.jpg" ofType:nil]; [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"image" fileName:@"666.jpg" mimeType:@"image/jpeg" error:nil]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }];

}


//下载文件

-(void)testDownloadFile

{ NSString *urlString = @"http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip";

//创建会话管理对象(通过默认配置)

AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

//返回文件保存的位置

NSString *path = [NSString stringWithFormat:@"%@/Documents/all.zip",NSHomeDirectory()]; NSLog(@"%@",NSHomeDirectory()); return [NSURL fileURLWithPath:path]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"下载完成"); }];

//启动任务

[task resume];

}


//监控网络状态

-(void)testMonitorNetworkStatus

{

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"www.baidu.com"]]; [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSDictionary *dict = @{ @(AFNetworkReachabilityStatusUnknown): @"未知", @(AFNetworkReachabilityStatusNotReachable): @"不可达", @(AFNetworkReachabilityStatusReachableViaWWAN): @"GPRS", @(AFNetworkReachabilityStatusReachableViaWiFi): @"Wifi", }; NSLog(@"状态为 %@",dict[@(status)]); }];

//开启状态监视

[manager.reachabilityManager startMonitoring];

}




- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值