iOS settings download partial files, how to get the full file size

In the requirements of video, when encountering such a requirement, when playing a video, it is necessary to pre-download the
next 10 videos, but only download the first 1M of the next ten videos

Implementation Method
1 Set cacheLength when creating a request

    resource = [[IdiotResource alloc] init];
        resource.requestURL = task.requestURL;
        resource.requestOffset = task.requestOffset;
        resource.fileLength = task.fileLength;
        resource.cachePath = task.cachePath;
        //预先下载1M
        resource.cacheLength = 1024 * 1024;
        resource.resourceType = IdiotResourceTypeNet;//网络资源
        [self.resources addObject:resource];

2. When creating a request, set the Range of the request header

- (void)fetchFromNetwork:(IdiotResource *)task withResource:(IdiotResource *)resource{
    
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[resource.requestURL originalSchemeURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    if (resource.cacheLength > 0) {
        [request addValue:[NSString stringWithFormat:@"bytes=%lld-%lld", MAX(resource.requestOffset, task.requestOffset + task.cacheLength), resource.requestOffset+resource.cacheLength-1] forHTTPHeaderField:@"Range"];
    }else{
        [request addValue:[NSString stringWithFormat:@"bytes=%lld-", resource.requestOffset] forHTTPHeaderField:@"Range"];
    }
    NSURLSessionDataTask * datatask = [self.session dataTaskWithRequest:request];
    datatask.taskDescription = [NSString stringWithFormat:@"%lld",task.requestOffset];
    [datatask resume];
    
    self.currentDataTask = datatask;
}

Three how to get the size of the complete file

In - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {The proxy method obtains the Content-Range of the response header field, note that it cannot be obtained through Content-Length, because at this time Content-Length is the size of the request part we set, not the complete size


- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
    
    IdiotResource * task = [self.taskDic objectForKey:dataTask.taskDescription];
    
    if (task.cancel) return;
    
    if (task.fileLength <= 0) {
        NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
        NSString * contentRange = [[httpResponse allHeaderFields] objectForKey:@"Content-Range"];
        NSString * fileLength = [[contentRange componentsSeparatedByString:@"/"] lastObject];
        task.fileLength = fileLength.integerValue > 0 ? fileLength.integerValue : response.expectedContentLength;
    }
    
    if (self.currentResource.fileLength <= 0) {
        self.currentResource.fileLength = task.fileLength;
    }
    
    if (!task.cachePath.length) {
        task.cachePath = [IdiotFileManager createSliceWithUrl:task.requestURL sliceName:[NSString stringWithFormat:@"%lld-%lld",task.requestOffset,task.fileLength]];
    }
    
    if (self.currentResource.cacheLength <= 0) {
        self.currentResource.cacheLength = task.fileLength - task.requestOffset;
    }
    
    completionHandler(NSURLSessionResponseAllow);
}

As shown below
Please add a picture description

Guess you like

Origin blog.csdn.net/LIUXIAOXIAOBO/article/details/132594925