TienLe
TienLe

Reputation: 623

AFNetworking: download + resume big file?

I'm using AFNetworking to download a big file. Here is my code. It work fine:

- (AFDownloadRequestOperation*)downloadBigFile:(BigFileObject*)bigFile
                             withCompleteBlock:(AFResultCompleteBlock)completeBlock
                                    errorBlock:(AFResultErrorBlock)errorBlock{

    NSString *name = [NSString stringWithFormat:@"%@", bigFile.name];
    NSString *link = [NSString stringWithFormat:@"%@", bigFile.link];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link]];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", name]];

    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        completeBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorBlock(error);
    }];


    [operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
        float progress = (float)totalBytesReadForFile / totalBytesExpectedToReadForFile;
        //block
        self.progressBlock(progress, totalBytesReadForFile, totalBytesExpectedToReadForFile);
    }];
    [operation start];

    return operation;
}

A file is being downloaded to local but not finished, take 30% finished for an example. Then stop it. Few day late, I resume it, but that link was not live. So, I request a new link and I wanna download from 30%. How to do it? My app is target iOS 6+;

Upvotes: 2

Views: 430

Answers (0)

Related Questions