26>ASI文件下载和上传

1.文件下载,支持断点续传

#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIView *progressView;
@property (nonatomic,assign) BOOL downloading;
@property (nonatomic,strong) ASIHTTPRequest *request;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.downloading) {
        [self.request clearDelegatesAndCancel];
        self.downloading = NO;
    }else{
    // 1. 创建url
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos/test.mp4"];
    // 2. 创建一个请求
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    // 3. 设置文件的缓存路径
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask , YES) lastObject];
    NSString *fileName = [caches stringByAppendingPathComponent:@"test.mp4"];
    request.downloadDestinationPath = fileName;
    // 4. 设置进度监听的代理(想要成为进度监听代理,最好遵守ASIProgressDelegate协议)
    request.downloadProgressDelegate = self.progressView;

    // 设置该属性为YES,就会支持断点下载
    request.allowResumeForFileDownloads = YES;
    // 如果要实现断点续传,需要设置一个文件的临时路径
    request.temporaryFileDownloadPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.temp"];

    // 5. 开始请求
    [request startAsynchronous];
    self.request = request;

    self.downloading = YES;
   }
}
@end

2.文件的上传

#import "HMViewController.h"
#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

// ASINetworkQueue *queue = [ASINetworkQueue queue];
// queue.shouldCancelAllRequestsOnFailure = YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.URL
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/upload"];

    // 2.创建一个请求对象
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    // 3.设置请求参数
    [request setPostValue:@"zhangsan" forKey:@"username"];
    [request setPostValue:@"123" forKey:@"pwd"];
    [request setPostValue:@"28" forKey:@"age"];
    [request setPostValue:@"1.89" forKey:@"height"];

    // 设置文件参数
    NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
    // 如果知道文件路径,最好就用这个方法(因为简单)
    // ASI内部会自动识别文件的MIMEType
    [request setFile:file forKey:@"file"];
// [request setFile:file withFileName:@"basic.pptx" andContentType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" forKey:@"file"];

    // 如果文件数据是动态产生的,就用这个方法(比如刚拍照完获得的图片数据)
// [request setData:<#(id)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>];

    // 4.设置监听上传进度的代理
    request.uploadProgressDelegate = self.progressView;

    // 5.开始请求
    [request startAsynchronous];

    // 6.监听完毕
    __weak typeof(request) weakRequest = request;
    [request setCompletionBlock:^{
        NSLog(@"%@", [weakRequest responseString]);
    }];
}
@end

你可能感兴趣的:(26>ASI文件下载和上传)