ASIHTTPRequest施工

先配置好ASIHTTPRequest(它们是MRC)

先在搜索asi相关的文件,全选双击添加        -fno-objc-arc

然后再搜索asi依赖的reachability的相关文件也同样的修改

修改完继续添加libz的框架。相关操作如下图




这些随便百度都有教程,虽然这个第三方有点历史。但使用起来还是比较方便

比起好过自己拼请求头,手动拼也比较容易出错

DownLoadFile(其缺点不能用response来拼suggesteFilename)劳烦告知我便捷的实现方法

Demo 


//  Created by gdarkness on 16/2/25.
//  Copyright © 2016年 gdarkness. All rights reserved.

#import "ViewController.h"
#import "ASIFormDataRequest.h"

@interface ViewController ()
@property(nonatomic,weak)IBOutlet UIProgressView *progressView;
@property(nonatomic,strong)ASIFormDataRequest *request;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(IBAction)pause:(id)sender{
    [self.request cancel];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self download];
}
-(void)download{
    NSURL *url = [NSURL URLWithString:@"http://localhost/av.mp4"];
    //创建请求
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    request.delegate = self;
    //下载进度的代理
    request.downloadProgressDelegate = self.progressView;
    //下载文件保存的路径
    request.downloadDestinationPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"av.mp4"];
    //断电续传
    request.allowResumeForFileDownloads = YES;
    //临时文件保护路径
    request.temporaryFileDownloadPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"av.mp4"];
    //开始异步下载
    [request startAsynchronous];
    self.request = request;
    //打印所在路径
    NSLog(@"%@",NSHomeDirectory());
}

UploadFile

Demo

-(void)upload{
    NSURL *url = [NSURL URLWithString:@"http://localhost/upload.php"];
    //创建请求
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    //要上传文件的路径
    NSString *path = [[NSBundle mainBundle]pathForResource:@"1.png" ofType:nil];
    [request addFile:path forKey:@"userfile"];
    __weak typeof (request)weakRequest = request;
    [request setCompletionBlock:^{
        NSLog(@"%@",weakRequest.responseString);
    }];
    //开始异步上传
    [request startAsynchronous];
}
-(void)dealloc{
    [self.request clearDelegatesAndCancel];
}

相比于自己手拼写强大多了。可惜有5year没作更新了。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值