AFNetworking downloadTaskWithRequest: progress: destination: completeHandler: do not write file to path

I am trying to upload a file using AFNetworking (2.5.4). The download is completed, the completion handler is called, with the error set to nil, everything looks fine, but the target file does not exist:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSString *fullPath = [valid path from my apps local manager] NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:req progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { return [NSURL URLWithString:fullPath]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { NSLog(@"Saved file to %@.", filePath); *** [[NSFileManager defaultManager] fileExistsAtPath:filePath.absoluteString] returns NO here *** }]; [cell.progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES]; [downloadTask resume]; 

The file path is the usual path available for my application:

/var/mobile/Containers/Data/Application/APP-GUID-REDACTED/Documents/FILE-NAME-REDACTED.docx

I used a different method before AFNetworking, and it could write to the same path just fine. The headers of the HTTP responses show everything perfectly (status 200, the correct length of the content, etc.), and if I twist the download URL, it downloads the file without any problems. No problem with the file.

Why is my destination file not written in the completion handler, despite the absence of errors?

UPDATE: I also tried AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; but nothing changes. I also tried to create an NSProgress pointer and send it for the progress argument, but to no avail.

+8
ios ios8 afnetworking-2
source share
2 answers

Use [NSURL fileURLWithPath:] (not URLWithString).

 return [NSURL fileURLWithPath:fullPath]; 
+15
source share

The problem here is the wrong file path or invalid file path . I had the same problem.

Create a path as shown below:

 NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; NSURL *filePathURL = [documentsDirectoryURL URLByAppendingPathComponent:[NSString stringWithFormat:@"your file name here",i]]; 

Now use the above path:

 NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:req progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { return filePathURL; } 
0
source share

All Articles