zoukankan      html  css  js  c++  java
  • AFN实现文件下载

    #import "ViewController.h"

    #import "AFNetworking.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

        [self download];

    }

     

    -(void)download

    {

        //1.创建会话管理者

        AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

        

        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];

        

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        

        //2.下载文件

        /*

         第一个参数:请求对象

         第二个参数:progress 进度回调 downloadProgress

         第三个参数:destination 回调(目标位置)

                    有返回值

                    targetPath:临时文件路径

                    response:响应头信息

         第四个参数:completionHandler 下载完成之后的回调

                    filePath:最终的文件路径

         */

        NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

            

            //监听下载进度

            //completedUnitCount 已经下载的数据大小

            //totalUnitCount     文件数据的中大小

            NSLog(@"%f",1.0 *downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);

            

        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

            

            NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];

            

            NSLog(@"targetPath:%@",targetPath);

            NSLog(@"fullPath:%@",fullPath);

            

            return [NSURL fileURLWithPath:fullPath];

        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

            

            NSLog(@"%@",filePath);

        }];

        

        //3.执行Task

        [download resume];

    }

     

     

    @end

  • 相关阅读:
    005.Kickstart部署多系统
    004.Kickstart部署之FTP架构
    003.Kickstart部署之HTTP架构
    C#并发编程之异步编程(二)
    设计模式之策略者模式
    设计模式之职责链模式
    C#并发编程之异步编程(一)
    C#并发编程之概述
    微服务探索与实践—总述
    设计模式之模板方法模式
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5480237.html
Copyright © 2011-2022 走看看