如何在AFNetworking 3中通过流下载大文件 [英] How do I download large file with streaming in AFNetworking 3

查看:115
本文介绍了如何在AFNetworking 3中通过流下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AFNetworking 3下载大文件。但是,当出现互联网中断或其他任何中断时,我需要恢复下载。如果在下载过程中出现中断,我想从较早停止的地方开始下载。是否可以使用AFNetworking或我使用其他任何库?请任何人帮助我。
这是我的代码。

I want to download large file using AFNetworking 3. But I need to resume download while there is any interruption like internet loss or any other things. If any interrupts while downloading I want to start download from where it stop early. Is it possible using AFNetworking or do I use any other libraries? Please anybody help me. Here is my code.

NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL];

//Watch the manager to see how much of the file it's downloaded
[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    //Convert totalBytesWritten and totalBytesExpectedToWrite into floats so that percentageCompleted doesn't get rounded to the nearest integer
    CGFloat written = totalBytesWritten;
    CGFloat total = totalBytesExpectedToWrite;
    CGFloat percentageCompleted = written/total;

    //Return the completed progress so we can display it somewhere else in app
    //progressBlock(percentageCompleted);
    NSLog(@"Percentage Completed : %f",percentageCompleted);
    [self updateProgressBar:percentageCompleted];
}];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    //Getting the path of the document directory
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"3511_1464694276.zip"];

    //If we already have a video file saved, remove it from the phone
    return fullURL;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    if (!error) {
        //If there's no error, return the completion block
        //completionBlock(filePath);
    } else {
        //Otherwise return the error block
        //errorBlock(error);
    }

}];

[downloadTask resume];


推荐答案

在此处了解更多信息: https://github.com/AFNetworking/AFNetworking

Read more about it here : https://github.com/AFNetworking/AFNetworking

然后尝试在您编写一些代码或自己尝试了一些操作后问问题。

And try to ask questions when you have written some code or tried something on your own.

下载进度文件:

- (IBAction)downloadAudio:(id)sender {



    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"http://101songs.com/fileDownload/Songs/0/26958.mp3"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

        dispatch_async(dispatch_get_main_queue(), ^{
            //Update the progress view
            [_myProgressView setProgress:downloadProgress.fractionCompleted];

        });



    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        // Do operation after download is complete

    }];
    [downloadTask resume];


}

这篇关于如何在AFNetworking 3中通过流下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆