在Cocoa中如何使用WebView实现文件上传?

7
WebView有一个叫做的方法:
- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener

但是几乎没有关于它的文档和细节。在内部,我显示一个打开文件对话框,并获取所选文件的名称。

像这样:

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

}

尽情享受!


你使用的编程语言是什么? - user586399
Objective-C,但现在我已经让它工作了。 - Dimillian
1
或者,将解决方案发布为答案,然后稍后接受您自己的答案(或更好的答案,如果有更好的答案出现)。 - Peter Hosey
runOpenPanelForFileButtonWithResultListener这个方法对我来说没有被调用。 - jkk
2个回答

8

我遵循了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
@jkk和其他遇到这个问题的人,你需要将视图连接到“UIDelegate”,否则该函数不会被调用。 - Oliver Cooper

2

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

  • 使用快速枚举而不是索引循环来遍历数组。它既快又易于阅读。只有当你真正需要索引时才应该使用索引循环,而这不是这种情况。
  • 你根本不需要第一个循环。向URLs数组发送valueForKey:消息,用@"relativePath"作为键。数组将询问每个对象(每个URL)的relativePath,收集所有结果的数组,并将其返回给你。这段代码只需要一行。
  • 你也不需要第二个循环。在10.6中,WebOpenPanelResultListener协议添加了chooseFilenames:,所以现在你只需要一次发送该消息,将整个数组传递给它即可。

非常感谢,我真的需要学会如何使用Objective-C的真正力量。 - Dimillian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接