【问题标题】:How to allow files uploading with WebView in Cocoa?如何允许在 Cocoa 中使用 WebView 上传文件?
【发布时间】:2011-11-29 09:36:09
【问题描述】:

WebView 有一个方法叫做

- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener

但是几乎有 0 个文档和详细信息。在里面我显示一个打开的文件对话框并获取选定的文件名。

像这样

- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
{       
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    [openDlg setCanChooseFiles:YES];

    [openDlg setCanChooseDirectories:NO];

    // process the files.
    if ( [openDlg runModal] == NSOKButton )
    {
        NSString* fileString = [[openDlg URL]absoluteString];
        [resultListener chooseFilename:fileString]; 
    }

}

然后呢?

我该怎么办?在网站上,它显示我选择了一个文件,但是当您单击上传网站时,只会返回一个错误,就像没有上传文件一样。我应该编写处理文件上传的代码还是什么?

我有点迷路了……

编辑:

事实上我得到了它的工作....只需从这里更改代码:Cocoa webkit: how to get file upload / file system access in webkit 一点,因为某些部分已被弃用

- (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];
    }

}

享受吧!

【问题讨论】:

  • 您使用的是什么语言?
  • Objective-c,但我现在可以工作了
  • 或者,将解决方案作为答案发布,然后稍后接受您自己的答案(或更好的答案,如果有的话)。
  • runOpenPanelForFileButtonWithResultListener 这个方法不适合我

标签: cocoa file upload webview uploading


【解决方案1】:

我关注了 Peter Hosey 的评论,哇,我的代码现在很短而且工作方式相同

- (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* files = [[openDlg URLs]valueForKey:@"relativePath"];
        [resultListener chooseFilenames:files];
    }

}

【讨论】:

  • runOpenPanelForFileButtonWithResultListener,我没有调用这个方法
  • @jkk 和其他遇到此问题的人,您需要将视图连接到“UIDelegate”,否则将不会调用该函数。
【解决方案2】:

有几种方法可以改进您的代码:

  • 要遍历数组,请使用fast enumeration 而不是索引循环。它更快更容易阅读。唯一应该使用索引循环的时候是真正需要索引的时候,而这种情况并非如此。
  • 您根本不需要第一个循环。向 URLs 数组发送 valueForKey: 消息,其中 @"relativePath" 为密钥。该数组将向每个对象(每个 URL)询问其relativePath,收集所有结果的数组,然后为您返回。用于此的代码是单行代码。
  • 您也不需要第二个循环。 WebOpenPanelResultListener 协议在 10.6 中添加了 chooseFilenames:,因此您现在只需发送该消息一次,将整个数组传递给它。

【讨论】:

  • 非常感谢,我真的需要学习使用Objective-C的真正力量
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 2014-04-09
  • 2020-12-06
  • 1970-01-01
  • 2018-10-16
相关资源
最近更新 更多