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

  • 相关阅读:
    inotifywait 目录或文件的监控
    outlook邮件中样式问题
    IE8兼容问题
    12px以下字体显示问题
    box-shadow
    cropper.js图片裁剪
    利用vcard和qrcode.js生成二维码导入联系人
    使用link rel="shortcut icon"为网页标题加图标
    将input或textarea设置为disabled的样式问题
    img没有src属性时自动出现边框
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5480237.html
Copyright © 2011-2022 走看看