我在服務器上有一個 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
uj5u.com熱心網友回復:
從iOS 14.5 開始,您可以WKDownloadDelegate用來下載回應中的附加檔案,并可以提供要保存的目標 URL:
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
self.webView.navigationDelegate = self;
NSURL* url = [NSURL URLWithString: @"https://your-domain.com/download.php"];
NSURLRequest* request = [NSURLRequest requestWithURL: url];
[self.webView loadRequest:request];
[self.view addSubview:self.webView];
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(nonnull void (^)(WKNavigationResponsePolicy))decisionHandler {
if (navigationResponse.canShowMIMEType) {
decisionHandler(WKNavigationResponsePolicyAllow);
} else {
decisionHandler(WKNavigationResponsePolicyDownload);
}
}
- (void)webView:(WKWebView *)webView navigationResponse:(WKNavigationResponse *)navigationResponse didBecomeDownload:(WKDownload *)download {
download.delegate = self;
}
#pragma mark - WKDownloadDelegate
- (void)download:(WKDownload *)download decideDestinationUsingResponse:(NSURLResponse *)response suggestedFilename:(NSString *)suggestedFilename completionHandler:(void (^)(NSURL * _Nullable))completionHandler {
// Save to Documents
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:suggestedFilename];
NSURL* url = [NSURL fileURLWithPath:filePath];
completionHandler(url);
}
- (void)downloadDidFinish:(WKDownload *)download {
// Downloaded
}
對于以前的 iOS版本,您應該手動下載回傳的檔案:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(nonnull void (^)(WKNavigationResponsePolicy))decisionHandler {
if (navigationResponse.canShowMIMEType) {
decisionHandler(WKNavigationResponsePolicyAllow);
}
else {
NSURL* downloadUrl = navigationResponse.response.URL;
NSURLSessionDataTask* dataTask = [NSURLSession.sharedSession dataTaskWithURL:downloadUrl completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
if (data != nil) {
// Save to Documents
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:navigationResponse.response.suggestedFilename];
[data writeToFile:filePath atomically:YES];
}
}];
[dataTask resume];
decisionHandler(WKNavigationResponsePolicyCancel);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358539.html
