输入类型=文件在OS X应用程序的WebView中不起作用 [英] Input type=file not working in WebView of OS X application

查看:124
本文介绍了输入类型=文件在OS X应用程序的WebView中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Swift中创建了一个OSX应用程序,并使用Xcode 7嵌入了一个WebView.我在WebView加载的网页上有一个文件选择器,它要求用户从用户的计算机中浏览特定文件.我面临的问题是,当用户单击浏览按钮时,什么也不会发生.

I have created an OSX application in Swift and embedded a WebView in it using Xcode 7. I have a file selector on the web page loaded by the WebView which asks the user to browse for a particular file from the user's computer. The problem I am facing is that nothing happens when the user clicks on the browse button.

如果我在Safari中打开相同的网页,则相同的文件选择器控件可以正常工作.

The same file selector control is working fine if i open the same web page in Safari.

我是新手,所以在这种情况下的帮助将不胜感激.

I am relatively new in swift, so help in this case would be appreciated.

这是我的viewDidLoad函数:

func viewDidLoad() {
    super.viewDidLoad() 
    let url=NSURL(string: "http://video.online-convert.com/convert-to-mp4")
    let request=NSURLRequest(URL:url!) 
    webView.frameLoadDelegate=self 
    webView.mainFrame.loadRequest(request)  
    webView.shouldCloseWithWindow = true 
    webView.drawsBackground = true 
}

谢谢.

推荐答案

我自己并没有为此工作,但是从令人费解的点点滴滴看来,这似乎可以满足您的要求.

I have not worked with this myself, but from puzzling bits and pieces together this seems to do what you'd like.

首先...我正在使用WKWebView.声明并使用本地HTML文件进行初始化,如下所示:

First...I'm using a WKWebView. That is declared and initialised with a local HTML file like so:

import Cocoa
import WebKit

class ViewController: NSViewController {

    let webview: WKWebView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        webview.autoresizingMask = [.viewWidthSizable, .viewHeightSizable]
        webview.frame = view.bounds
        webview.uiDelegate = self
        view.addSubview(webview)

        let fileURL = URL(fileURLWithPath: "/Users/myuserhere/Desktop/index.html")
        webview.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
     }
  }

有趣的部分是webview.uiDelegate.这保证我们将遵守WKUIDelegate协议此处记录的.如它所说:

The interesting part is webview.uiDelegate. This promises that we will conform to the WKUIDelegate protocol documented here. As it says:

WKUIDelegate类提供了用于代表网页呈现本机用户界面元素的方法.

The WKUIDelegate class provides methods for presenting native user interface elements on behalf of a webpage.

您可以实现的方法之一是 runOpenPanelWithParameters

One of the methods you can implement is runOpenPanelWithParameters:

如果实现此方法,则保证将显示一个文件上传面板,并使用用户选择的结果调用此方法的callback方法. 记住,当用户按下cancel时也调用callback方法.

If you implement this method, you promise that you will present a file upload panel and call the callback method of this method with the outcome of what the user selected. Remember to also call the callback method when the user presses cancel.

这是一个快速而肮脏的例子:

Here is a quick and dirty example:

extension ViewController: WKUIDelegate {
    func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) {
        let openPanel = NSOpenPanel()
        openPanel.canChooseFiles = true
        openPanel.begin { (result) in
            if result == NSApplication.ModalResponse.OK {
                if let url = openPanel.url {
                    completionHandler([url])
                }
            } else if result == NSApplication.ModalResponse.cancel {
                completionHandler(nil)
            }
        }
    }
}

希望这会给您一些入门.

Hopefully that gives you something to get started with.

以下一些链接对我有帮助:

Here are some links that helped me:

如何从WKWebView上传文件

如何实现委托方法

如何在Swift中创建NSOpenPanel

我尝试将一个简单的程序粘合在一起,以在iOS上执行相同的操作.我希望它对您有用@DarshanMothreja

I tried gluing together a simple program to do the same on iOS. I hope it is useful to you @DarshanMothreja

HTML 名为index.html的文件已添加到Xcode项目.内容如下:

HTML A file called index.html is added to the Xcode project. The content looks like this:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <label for="file">File goes here</label>
    <input type="file" name="file" value="File">
  </body>
</html>

Swift

Swift

这是ViewController

import UIKit
import WebKit

class ViewController: UIViewController {
    let webview: WKWebView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        webview.autoresizingMask = [ .flexibleWidth, .flexibleHeight ]
        webview.frame = view.bounds
        webview.uiDelegate = self
        view.addSubview(webview)   

        if let path = Bundle.main.path(forResource: "index", ofType: "html") {
            let fileURL = URL(fileURLWithPath: path)
            webview.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
        }
    }
}

extension ViewController: WKUIDelegate { }

如果运行上述命令,则在点击选择文件"按钮时会得到此结果,而无需添加任何委托方法.

If I run the above, I get this result when I tap the "Choose File" button, no need to add any delegate methods.

希望能给您一些帮助.

这篇关于输入类型=文件在OS X应用程序的WebView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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