Ios 如果下载被取消,则删除未完成的下载文件

Ios 如果下载被取消,则删除未完成的下载文件,ios,afnetworking,afhttprequestoperation,Ios,Afnetworking,Afhttprequestoperation,我正在使用AFNetworking下载文件,使用以下代码: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadFileUrl]]; AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSArray * paths = NSSearchPath

我正在使用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];
但是,未完成的下载文件仍保留在目录中。
请告诉我删除未完成下载文件的解决方案。

尝试此方法删除文件-

NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentFolderPath = [searchPaths objectAtIndex:0];
BOOL success=[[NSFileManager defaultManager] fileExistsAtPath: targetPath];
 if (success)
 {
      BOOL deletedFile = [[NSFileManager defaultManager] removeItemAtPath: targetPath error:&error];
     if (deletedFile)
        NSLog(@"File deleted");
     else
        NSLog(@"Not able to delete File");
}

请尝试删除此文件-

NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentFolderPath = [searchPaths objectAtIndex:0];
BOOL success=[[NSFileManager defaultManager] fileExistsAtPath: targetPath];
 if (success)
 {
      BOOL deletedFile = [[NSFileManager defaultManager] removeItemAtPath: targetPath error:&error];
     if (deletedFile)
        NSLog(@"File deleted");
     else
        NSLog(@"Not able to delete File");
}

试试这个代码。我没有检查

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

    [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidSuccessNotification" object:nil];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSFileManager *fileMgr = [NSFileManager defaultManager] ;
      NSError *error = nil;
     BOOL removeSuccess = [fileMgr removeItemAtPath:targetPath error:&error];
        if (!removeSuccess) {
            // Error handling
            ...
        }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidFailedNotification" object:nil];

}];

试试这个代码。我没有检查

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

    [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidSuccessNotification" object:nil];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSFileManager *fileMgr = [NSFileManager defaultManager] ;
      NSError *error = nil;
     BOOL removeSuccess = [fileMgr removeItemAtPath:targetPath error:&error];
        if (!removeSuccess) {
            // Error handling
            ...
        }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidFailedNotification" object:nil];

}];

当您取消下载操作或下载操作失败时,您可以获取文件的路径并将其从目录中删除。当您取消下载操作或下载操作失败时,您可以获取文件的路径并将其从目录中删除。