【问题标题】:Cocoa webkit: how to get file upload / file system access in webkitCocoa webkit:如何在 webkit 中获取文件上传/文件系统访问权限
【发布时间】:2011-03-03 06:57:24
【问题描述】:

我从 WebKit 框架创建了一个自定义浏览器。我几乎把所有东西都设置好了。

但是,当我访问带有文件上传的网页(比如 flickr)时,当我按下“上传”按钮时没有任何反应。通常这会在 safari/firefox/.. 中弹出一个窗口。

我需要什么才能让文件上传以在 Cocoa 中使用 WebKit? NSFileHandler,NSFileManager?我该怎么做?

问候, 弗里斯加德

【问题讨论】:

    标签: cocoa file-upload webkit


    【解决方案1】:

    好吧,我自己想通了。

    在一个类中实现以下方法。在 Interface Builder 中将 WebView 的类设置为 UIDelegate

    (如何使用NSOpenPanel 我在这里找到: http://ekle.us/index.php/2006/12/displaying_a_file_open_dialog_in_cocoa_w )

    - (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
    {       
        // Create the File Open Dialog class.
        NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    
        // Enable the selection of files in the dialog.
        [openDlg setCanChooseFiles:YES];
    
        // Enable the selection of directories in the dialog.
        [openDlg setCanChooseDirectories:NO];
    
        // Display the dialog.  If the OK button was pressed,
        // process the files.
        if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
        {
            // Get an array containing the full filenames of all
            // files and directories selected.
            NSArray* files = [openDlg filenames];
    
            // Loop through all the files and process them.
            //for(int i = 0; i < [files count]; i++ )
            {
                NSString* fileName = [files objectAtIndex:0]; //i]; 
                // Do something with the filename.
                [resultListener chooseFilename:fileName]; 
            }
        }
    
    }
    

    ps。对多个文件使用[resultListener chooseFilenames:...]

    【讨论】:

    • 我用这个,我可以选择文件但是然后呢?当我单击 webview 中的上传按钮时,它只会说错误。我需要处理文件上传机制吗?
    • 应该由UIDelegate来处理,也许你还要设置WebView的委托指向同一个类?你得到什么错误,任何错误代码?
    • 我刚刚检查了我的代码。我唯一要做的就是[webView setUIDelegate:webFileUploader] webFileUploader 包含上面的代码。
    • runOpenPanelForFileButtonWithResultListener 当我点击输入文件按钮时没有调用这个方法。我已经在界面生成器本身中连接了 uielegate
    • 用您的代码创建一个新问题,然后更容易发现任何错误
    【解决方案2】:

    随着表单部分的折旧,我通过稍微改变它来让它工作:

    - (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
    {       
        // Create the File Open Dialog class.
        NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    
        // Enable the selection of files in the dialog.
        [openDlg setCanChooseFiles:YES];
    
        // Enable the selection of directories in the dialog.
        [openDlg setCanChooseDirectories:NO];
    
        if ( [openDlg runModal] == NSOKButton )
        {
            NSArray* URLs = [openDlg URLs];
            NSMutableArray *files = [[NSMutableArray alloc]init];
            for (int i = 0; i <[URLs count]; i++) {
                NSString *filename = [[URLs objectAtIndex:i]relativePath];
                [files addObject:filename];
            }
    
            for(int i = 0; i < [files count]; i++ )
            {
                NSString* fileName = [files objectAtIndex:i];
                [resultListener chooseFilename:fileName]; 
            }
            [files release];
        }
    
    }
    

    【讨论】:

    • runOpenPanelForFileButtonWithResultListener 当我点击输入文件按钮时没有调用这个方法。我已经在界面生成器本身中连接了 uielegate
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2014-08-04
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多