开发者

Cocoa webkit: how to get file upload / file system access in webkit

开发者 https://www.devze.com 2023-02-14 07:53 出处:网络
I created a custom browser from the WebKit framework. I almost got everything setup. However, when visiting a webpage with file upload (say flickr), nothing happens when I press the \"Upload\" button

I created a custom browser from the WebKit framework. I almost got everything setup.

However, when visiting a webpage with file upload (say flickr), nothing happens when I press the "Upload" button. Normally this would give an popup in safari/firefox/..

What do I need to get file upload to work with WebKit in Cocoa? NSFileHandler, NSFileManager? And h开发者_StackOverflowow do I do it?

Regards, Friesgaard


Alright, I figured I out for my self.

Implement the following method in a class. Set the class as as UIDelegate for the WebView in Interface Builder.

(How to use a NSOpenPanel I found here: 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. use [resultListener chooseFilenames:...] for multiple files


I got it working by altering this a bit as form part is depreciated:

- (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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号