【发布时间】:2021-12-23 22:50:55
【问题描述】:
我在服务器上有一个 php 文件,当我通过任何网络浏览器访问它时,它会生成一个要下载的文件,即使是 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);
我在我的应用程序中使用 WKwebview,我真正想做的是下载它并存储在我的应用程序沙箱文件夹中。 我尝试了多种下载文件的方法,但 WKwebview 总是下载 *.php 文件,而不是生成 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];
有办法下载吗? (在 Objective-c 中)
谢谢!
更新: 目前,实现这一点,我可以在 wkwebview 中以文本模式看到文件,但仍然无法下载:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
【问题讨论】:
-
很可能是服务器配置问题(不执行 php、没有 php 处理程序等)。尝试使用浏览器、curl 或其他任何工具,如果您得到相同的结果,请检查您的网络服务器配置。
-
@YvesLeBorg,它适用于我们的 Android 应用 Webview、safari iOS 浏览器和所有网络浏览器..
标签: php ios objective-c download wkwebview