AFNetworking:下载+恢复大文件?

时间:2015-03-19 09:49:48

标签: ios iphone afnetworking-2 downloadfile resume-download

我使用AFNetworking下载大文件。这是我的代码。它工作正常:

- (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;
}

正在将文件下载到本地但尚未完成,以30%完成为例。然后停下来。几天晚了,我恢复了,但那个链接不是活的。所以,我要求一个新的链接,我想从30%下载。怎么做? 我的应用程序是目标iOS 6 +;

0 个答案:

没有答案