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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值