当前位置:   article > 正文

iOS 文件下载 (AFNetwork 三方框架 含progressView)五_afnetwork下载附件tableview

afnetwork下载附件tableview

1.创建request

  1. - (void)download2
  2. {
  3. NSString *urlString = @"http:192.168.0.179:8080/Myweb/download.do";
  4. urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  5. NSLog(@"hello");
  6. NSURL *url = [NSURL URLWithString:urlString];
  7. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  8. [[self.session downloadTaskWithRequest:request] resume];
  9. }

最后一句
[[self.session downloadTaskWithRequest:request] resume];

的session,本文使用懒加载

2. session懒加载,并添加代理,监听文件下载情况

  1. // 懒加载
  2. - (NSURLSession *)session
  3. {
  4. if(_session == nil)
  5. {
  6. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  7. _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
  8. }
  9. return _session;
  10. }

3.进度更新和label下载完成百分比更新(注意,一定要在主线程更新,不然无法显示)

  1. /* Sent periodically to notify the delegate of download progress. */
  2. /**
  3. [""] * @brief 更新进度条,使用此代理
  4. [""] *
  5. [""] * @param session session
  6. [""] * @param downloadTask 下载任务
  7. [""] * @param bytesWritten 当前写入bytes
  8. [""] * @param totalBytesWritten 当前总共写入bytes
  9. [""] * @param totalBytesExpectedToWrite 期望写入的总bytes
  10. [""] *
  11. [""] * @return <#return value description#>
  12. [""] */
  13. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
  14. didWriteData:(int64_t)bytesWritten
  15. totalBytesWritten:(int64_t)totalBytesWritten
  16. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
  17. {
  18. float progress = (float)totalBytesWritten / totalBytesExpectedToWrite;
  19. NSLog(@"%f", progress);
  20. //主线程更新UI
  21.  dispatch_async(dispatch_get_main_queue(), ^(void){
  22. self.progressLabel.text=[NSString stringWithFormat:@"%%%.0f",progress*100];
  23. [self.progressView setProgress:progress animated:YES];
  24. });
  25. }

4. 下载完成,将下载的数据写入指定缓存路径。

  1. /**
  2. [""] * @brief The delegate should copy or move the file at the given location to a new location as it will be
  3. removed when the delegate message returns.
  4. [""] *
  5. [""] * @param session session description
  6. [""] * @param downloadTask downloadTask 下载任务
  7. [""] * @param location 下载文档位置(临时)
  8. [""] *
  9. [""] * @return
  10. [""] */
  11. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
  12. didFinishDownloadingToURL:(NSURL *)location
  13. {
  14. NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
  15. NSString *retStr = [downloadTask.response.suggestedFilename stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  16. NSLog(@"%@ \n",retStr);
  17. NSString *path = [cacheDir stringByAppendingPathComponent:retStr];
  18. NSLog(@"%@",path);
  19. //NSURL *url2=location;
  20.  NSLog(@"%@",location);
  21. NSData *mydata=[NSData dataWithContentsOfURL:location];
  22. [mydata writeToFile:path atomically:YES];
  23. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/629908
推荐阅读
相关标签
  

闽ICP备14008679号