如何从 os x 中的 webview 下载文件并将其保存到特定位置 [英] How to download a file from the webview in os x and save it to a specific location

查看:82
本文介绍了如何从 os x 中的 webview 下载文件并将其保存到特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处找都没有找到我要找的东西,所以这是我的问题:

I have looked everywhere and have not found exactly what I am looking for, so here is my question:

我正在玩一个基本的应用程序.我已经创建了一个 webview,希望能够从 webview 中加载的网站下载一个文件,并将文件保存到本地机器上的 Downloads 文件夹中.该站点在 webview 中加载良好,现在如何下载文件,例如从站点下载 .xml 文件并将其保存到本地计算机上的 Downloads 文件夹中?

I have a basic app I am playing around with. I have created a webview and would like to be able to download a file from the website that loads in the webview and save the file to say the Downloads folder on the local machine. The site loads fine inside the webview, now how do I download a file, say an .xml file from the site and save it to the Downloads folder on the local machine?

这是我目前所拥有的:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];//<-- example site
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[webView mainFrame] loadRequest:request];



}

我希望能够下载文件(可以使用委托),然后将其保存到本地计算机上的某个位置.我对此很陌生,因此非常感谢您的帮助.

I would like to be able to download a file (possible using a delegate) then save it to a location on the local computer. I am pretty new to this so I'd appreciate any help.

推荐答案

问题已解决.我添加了以下代码以使其工作:

The issue has been resolved. I added the following code to make it work:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; // <-- example website
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    self.webView.policyDelegate = self;
    [self.webView setDownloadDelegate:self];
    [[self.webView mainFrame] loadRequest:request];

}

- (void)webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type
                                                    request:(NSURLRequest *)request
                                                    frame:(WebFrame *)frame
                                            decisionListener:(id < WebPolicyDecisionListener >)listener
{
    if([type isEqualToString:@"application/octet-stream"]) //this is the type I was looking for
    {
        //figure out how to save file here

        [listener download];
        NSURLDownload *downLoad = [[NSURLDownload alloc] initWithRequest:request delegate:self];

        if(downLoad)
        {
            [self download:downLoad decideDestinationWithSuggestedFilename:@"filename.ext"];
            NSLog(@"File Downloaded Succesfully");
            //[webView close];
            [self.window close];

        }
        else
        {
            NSLog(@"The download failed");
        }



    }
    //just ignore all other types; the default behaviour will be used
}

-(void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
    NSString *destinationFileName;
    NSString *homeDirectory = NSHomeDirectory();

    destinationFileName = [[homeDirectory stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:filename];
    [download setDestination:destinationFileName allowOverwrite:NO]; //file is being saved to the Documents folder on the local machine
}

希望这对其他人有帮助.

Hope this will be helpful to someone else.

这篇关于如何从 os x 中的 webview 下载文件并将其保存到特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆