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

  • 相关阅读:
    使用 Selenium
    Senium 简介
    第8章 动态渲染页面爬取
    Ajax 结果提取
    Ajax 分析方法
    WINDOW 2008多人访问设置
    Windows 2012设置允许单个用户连接多个会话的方法
    Windows Server 2012开启多人远程
    BOM 表
    修复材料工单分配材料订单重复占料问题的开发
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5480237.html
Copyright © 2011-2022 走看看