2011-10-02 258 views
7

的WebView有一个方法叫如何允许在Cocoa中使用WebView上传文件?

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

但几乎为0 doc和它的细节。在里面我显示一个打开的文件对话框并获取选定的文件名。

像这样

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

} 

享受!

+0

什么语言你使用? – 2011-10-02 13:05:08

+0

Objective-c,但我现在正在工作 – Dimillian

+0

然后删除你的问题.. – 2011-10-02 15:26:31

回答

8

我跟彼得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]; 
    } 

} 
+0

的真正力量runOpenPanelForFileButtonWithResultListener,这个方法不会为我调用 – jkk

+0

@jkk和其他任何有这个问题的人,你需要连接'UIDelegate'的视图,否则该函数将不会被调用。 –

2

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

  • 要遍历数组,使用fast enumeration而不是索引循环。阅读起来既快又简单。唯一一次你应该使用索引循环的时候,你确实需要索引,而这不是这种情况。
  • 根本不需要第一个循环。发送URLs数组一个valueForKey:消息,其中@"relativePath"作为密钥。该数组将询问每个对象(每个URL)的relativePath,收集所有结果的数组,并返回给您。这个代码是一行代码。
  • 你也不需要第二个循环。 WebOpenPanelResultListener协议在10.6中增加了chooseFilenames:,所以你现在可以只发送一次该消息,将整个数组传递给它。
+0

非常感谢,我真的需要学习使用Objective-C – Dimillian