I have a php file on the server that generates a file to download when I access to it via any web browser, even with safari.
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/text');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
I'm using the WKwebview in my app, and what I really want to do is to download it and store is in my app sandbox folder. I have tried multiple ways to download the file but the WKwebview always download the *.php file, not the file that generates de php.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURL]];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSString *aux= [response suggestedFilename];
//return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
return [documentsDirectoryURL URLByAppendingPathComponent:@"db.bk"];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
There is a way to download it? (in Objective-c)
Thanks!
UPDATE: currently, Implementing this, I can see the file in text mode in the wkwebview, but still not be able to download it:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler