我正在使用 AFNetworking 下载文件,使用以下代码:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadFileUrl]];
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentDirectory = [paths objectAtIndex:0];
NSString * targetFileName = [downloadFileUrl lastPathComponent];
NSString * targetPath = [documentDirectory stringByAppendingPathComponent:targetFileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidSuccessNotification" object:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidFailedNotification" object:nil];
}];
[operation start];
我向用户提供取消下载的选项,我这样做是:
[operation cancel];
但是,未完成的下载文件仍保留在目录中。请让我知道删除未完成下载文件的解决方案。