zoukankan      html  css  js  c++  java
  • iOS 文件下载

    iOS 视频音乐类等应用会用到“文件下载”。文件下载在iOS中的实现如下:

    1、小文件下载

    @interface ViewController () <NSURLConnectionDataDelegate>
    
    @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
    /** 文件数据 */
    @property (nonatomic, strong) NSMutableData *fileData;
    /** 文件的总长度 */
    @property (nonatomic, assign) NSInteger contentLength;
    @end
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"];
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
    }
    
    #pragma mark - <NSURLConnectionDataDelegate>
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
    {
        self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue];
        self.fileData = [NSMutableData data];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [self.fileData appendData:data];
        CGFloat progress = 1.0 * self.fileData.length / self.contentLength;
        NSLog(@"已下载:%.2f%%", (progress) * 100);
        self.progressView.progress = progress;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"下载完毕");
        
        // 将文件写入沙盒中
        
        // 缓存文件夹
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        // 文件路径
        NSString *file = [caches stringByAppendingPathComponent:@"minion_15.mp4"];
        
        // 写入数据
        [self.fileData writeToFile:file atomically:YES];
        self.fileData = nil;
    }

    2、大文件下载

    #define XMGFile [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"minion_15.mp4"]
    
    @interface ViewController () <NSURLConnectionDataDelegate>
    @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
    /** 文件的总长度 */
    @property (nonatomic, assign) NSInteger contentLength;
    /** 当前下载的总长度 */
    @property (nonatomic, assign) NSInteger currentLength;
    
    /** 文件句柄对象 */
    @property (nonatomic, strong) NSFileHandle *handle;
    
    @end
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"];
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
    }
    #pragma mark - <NSURLConnectionDataDelegate>
    /**
     * 接收到响应的时候:创建一个空的文件
     */
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
    {
        // 获得文件的总长度
        self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue];
        
        // 创建一个空的文件
        [[NSFileManager defaultManager] createFileAtPath:XMGFile contents:nil attributes:nil];
        
        // 创建文件句柄
        self.handle = [NSFileHandle fileHandleForWritingAtPath:XMGFile];
    }
    
    /**
     * 接收到具体数据:马上把数据写入一开始创建好的文件
     */
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        
        // 指定数据的写入位置 -- 文件内容的最后面
        [self.handle seekToEndOfFile];
        
        // 写入数据
        [self.handle writeData:data];
        
        // 拼接总长度
        self.currentLength += data.length;
        
        // 进度
        self.progressView.progress = 1.0 * self.currentLength / self.contentLength;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        // 关闭handle
        [self.handle closeFile];
        self.handle = nil;
        
        // 清空长度
        self.currentLength = 0;
    }
  • 相关阅读:
    .NET中非对称加密RSA算法的密钥保存
    WGS84经纬度坐标到北京54高斯投影坐标的转换[转]
    [APPS] HTC Footprints & HTC Locations for MikG 2.x Read more:
    firefox+ssh无法看youtube视频的解决方案
    【转】sdemon命令实践
    How to share a custom ArcMap command (DLL)
    【转】sdemon命令实践
    红旗桌面版本最新运用法子和结果解答100例8
    红旗Linux桌面4.1文本安装过程图解(二)
    Ubuntu把在效能器范畴起更重要的脚色
  • 原文地址:https://www.cnblogs.com/jukaiit/p/5622611.html
Copyright © 2011-2022 走看看