用AFNetworking实现断点续传,暂停,继续

  1. //获取已下载的文件大小  
  2. - (unsigned long long)fileSizeForPath:(NSString *)path {  
  3.     signed long long fileSize = 0;  
  4.     NSFileManager *fileManager = [NSFileManager new]; // default is not thread safe  
  5.     if ([fileManager fileExistsAtPath:path]) {  
  6.         NSError *error = nil;  
  7.         NSDictionary *fileDict = [fileManager attributesOfItemAtPath:path error:&error];  
  8.         if (!error && fileDict) {  
  9.             fileSize = [fileDict fileSize];  
  10.         }  
  11.     }  
  12.     return fileSize;  
  13. }  
  14. //开始下载  
  15. - (void)startDownload {  
  16.     NSString *downloadUrl = @"http://www.xxx.com/xxx.zip";  
  17.     NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  18.     NSString *downloadPath = [cacheDirectory stringByAppendingPathComponent:@"xxx.zip"];  
  19.     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadUrl]];  
  20.     //检查文件是否已经下载了一部分  
  21.     unsigned long long downloadedBytes = 0;  
  22.     if ([[NSFileManager defaultManager] fileExistsAtPath:downloadPath]) {  
  23.     //获取已下载的文件长度  
  24.         downloadedBytes = [self fileSizeForPath:downloadPath];  
  25.         if (downloadedBytes > 0) {  
  26.             NSMutableURLRequest *mutableURLRequest = [request mutableCopy];  
  27.             NSString *requestRange = [NSString stringWithFormat:@"bytes=%llu-", downloadedBytes];  
  28.             [mutableURLRequest setValue:requestRange forHTTPHeaderField:@"Range"];  
  29.             request = mutableURLRequest;  
  30.         }  
  31.     }  
  32.     //不使用缓存,避免断点续传出现问题  
  33.     [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];  
  34.     //下载请求  
  35.     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  36.     //下载路径  
  37.     operation.outputStream = [NSOutputStream outputStreamToFileAtPath:downloadPath append:YES];  
  38.     //下载进度回调  
  39.     [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {  
  40.         //下载进度  
  41.         float progress = ((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes);  
  42.     }];  
  43.     //成功和失败回调  
  44.     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  45.               
  46.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  47.               
  48.     }];  
  49.     [operation start];  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值