1

我正在尝试在此站点上以编程方式下载文件,我发现当您单击突出显示的下载按钮(“下载”)时,它会运行 Javascript:document.getElementById('downLoad').action='/download.php?fileid=11024011';downishare('0');

在我的 Mac 上,它运行良好并在我在 Safari 上运行时下载文件。但是当我使用

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('downLoad').action='/download.php?fileid=11024011'"]];

它不返回任何东西。

有谁知道为什么以及如何获得下载网址?

谢谢。

4

2 回答 2

0

您是否尝试过实现 webView 委托方法shouldStartLoadWithRequest来解析链接?


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
if ([[[request URL] absoluteString] hasPrefix:@"http://ishare.iask.sina.com.cn/download.php?fileid="])
//I believe this would download everything that has a link prefix of "http://ishare.iask.sina.com.cn/download.php?fileid="
//So you should create some sort of checker to make sure it only downloads the file they select
        // Determile cache file path
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];   
        // Download and write to file
        NSURL *url = [NSURL URLWithString:[[request URL] absoluteString]];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        [urlData writeToFile:filePath atomically:YES];

        // Load file in UIWebView
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

        return NO;
    }
    else {
        return YES;
    }
于 2012-12-18T19:26:30.563 回答
0

嗯……下载地址是这样的: http://ishare.iask.sina.com.cn/download.php?fileid= 11024011

您也可以使用 chrome 中的“网络”选项卡或 firefox 中的 firebug 进行检查。但是...当您尝试直接访问它时,它会重定向您。可能有一些服务器端的请求验证,比如引用 URL 或其他东西。

于 2012-12-04T13:21:09.337 回答