7

我正在使用队列使用 AFNetworking 下载一些文件。这是我的代码:

apiClient =[[AFHTTPClient alloc]initWithBaseURL: [NSURL URLWithString:ZFREMOTEHOST]];
    for (NSString *element in self.productsArray) {
            NSURL *url = [NSURL URLWithString:element];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            op = [[AFHTTPRequestOperation alloc] initWithRequest:request];

            NSString *documentsDirectory = nil;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            documentsDirectory = [paths objectAtIndex:0];

            NSString *targetFilename = [url lastPathComponent];
            NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:targetFilename];

            op.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];

            [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                //failure case
                NSLog(@"BaaZ  File NOT Saved %@", targetPath);

                //remove the file if saved a part of it!
                NSFileManager *fileManager = [NSFileManager defaultManager];
                [fileManager removeItemAtPath:targetPath error:&error];

                if (error) {
                    NSLog(@"error dude");
                }

                if ([operation isCancelled]) {
                    //that doesn't work.
                    NSLog(@"Canceled");
                }
            }];

            [op setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
                if (totalBytesExpectedToRead > 0) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.progressView.alpha = 1;
                        self.progressView.progress = (float)totalBytesRead / (float)totalBytesExpectedToRead;
                        NSString *label = [NSString stringWithFormat:@"Downloaded %lld of %lld bytes", totalBytesRead,totalBytesExpectedToRead];
                        self.progressLabel.text = label;
                    });
                }
            }];
        [self.resourcesArray addObject:op];

        }
    for (AFHTTPRequestOperation *zabols in self.resourcesArray) {
        [apiClient.operationQueue addOperation:zabols];
    }

该代码在文件下载时运行良好,但我想要一些取消功能,所以我有一个带有以下代码的操作按钮:

[apiClient.operationQueue cancelAllOperations];

操作取消文件,但 Documents 文件夹中有一些垃圾文件。通过说垃圾,我的意思是开始下载的文件我取消了它们,我得到了一个同名但无用的文件,因为它已损坏而无法打开。

我怎样才能防止 AF 这样做并在我取消它时只保留已完成的文件?

任何帮助将不胜感激。

还尝试通过这样的工作取消工作:

for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) {
        [ap cancel];
        NSLog(@"%@",ap.responseFilePath);
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:ap.responseFilePath error:nil];

    }

并删除垃圾文件,但这也不起作用。

4

2 回答 2

7

尝试使用AFDownloadRequestOperation扩展,它还支持恢复部分下载,使用临时目录并具有帮助计算正确下载进度的特殊块。

于 2013-01-02T22:57:33.360 回答
-1

You can also try that : github

于 2013-02-26T12:05:12.117 回答