【问题标题】:OS X webview get actual filename stringOS X webview 获取实际的文件名字符串
【发布时间】:2015-11-12 18:16:47
【问题描述】:

感谢下面的链接,我已经在 web 视图中成功实现了 NSURLDownload。在代码中,我必须指定一个文件名。我宁愿使用正在下载的文件的文件名。我如何获得该文件名?

How to download a file from the webview in os x and save it to a specific location

【问题讨论】:

    标签: objective-c macos webview


    【解决方案1】:

    可以从请求参数中解析出来,也可以和调用代码分开传入。

    【讨论】:

    • 我希望有一个简单的事情我可以打电话来得到这个。我什至不能使用下面的代码,因为某些下载 url 不以文件名结尾。 NSString *filename = [[[请求 URL] absoluteString] lastPathComponent];
    【解决方案2】:

    您可以从NSURLDownloadDelegate 委托实现download:decideDestinationWithSuggestedFilename: 方法。

    类似这样的:

    // somewhere in your code
    myWebView.downloadDelegate = self;
    
    - (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename {
    
    
        NSString *destinationFilename = [[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"]
                               stringByAppendingPathComponent:filename];
        [download setDestination:destinationFilename allowOverwrite:YES];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多