0
我下載與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];
但是,未完成的下載文件仍保留在目錄中。 請讓我知道刪除未完成的下載文件的解決方案。
當您取消下載操作或下載操作失敗時,您可以獲取文件的路徑並將其從目錄中刪除。 –